﻿function initialize(){
    $(".flashAlternateNav").hide();
    $(".nav-news").newsPages(5);
    $(".modal .block").click(function(evt){ 
		$(this.parentNode).find(".inner").fadeOut(250,function(){$(this.parentNode).hide()})}
	);
  	var $media = $(".media").media({caption:false,params:{autoplay:true}});
	//$("#panel-content,#panel-intro,.cap-border").contentBorder();
    wireVendors();
   	$(".nav .vendor-location a").click(
	    function (){
	        $(".panel-body").load(this.href+"/StoreLocatorStub", null, vendorLoad);
	        return false;
	  }
	 );
	$(".subsection p").truncate();
	
	setTimeout(fixSifr,250);
    
}

fixSifr = function(){
	$(".sIFR-flash").each(
		function(){
			//var div = document.createElement("br");
			//this.parentNode.appendChild(div);
			this.parentNode.style.height = this.offsetHeight+"px";	
			this.parentNode.style.width = this.offsetWidth+"px";
		}
	)
	$("#panel-content,#panel-intro,.cap-border").contentBorder();
	
}

function wireVendors(){
   $(".vendor-list li>a").click(
    function (){
        $(".panel-body").load(this.href+"/StoreLocatorStub", null, vendorLoad);
        return false;
    }
   );
   
   $(".vendor-list li.vendor a").click(
    function (evt){
        $(".modal .inner").load(this.href+"/Vendor .panel-body *", null,openModal);
        return false;
    }
   );
   /*$("a.vendor").click(
    function (){
        $(".panel-body").load(this.href+"/StoreLocatorStub", null, vendorLoad);
        return false;
    }
   );*/
}

function openModal(responseText, textStatus, XMLHttpRequest){
	var $modal = $(".modal").vCenter().show().find(".inner").slideDown(250);
}

function vendorLoad(responseText, textStatus, XMLHttpRequest) {
    wireVendors();
}


$(document).ready(initialize);
/*
(function($){
   $.fn.vCenter = function(options) {
     var pos = {
       sTop : function() {
         return window.pageYOffset || $.boxModel &&
document.documentElement.scrollTop ||   document.body.scrollTop;
       },
       wHeight : function() {
         if ( $.browser.opera || ($.browser.safari && parseInt
($.browser.version) > 520) ) {

return window.innerHeight - (($ (document).height() >

window.innerHeight) ? getScrollbarWidth() : 0);
                } else if ( $.browser.safari ) {
                        return window.innerHeight;
                }       else {
           return $.boxModel && document.documentElement.clientHeight
|| document.body.clientHeight;
         }
       }
     };
     return this.each(function(index) {
       if (index == 0) {
         var $this = $(this);
         var elHeight = $this.height();
         $this.css({
           position: 'absolute',
           marginTop: '0',
           top: pos.sTop() + (pos.wHeight() / 2) - (elHeight / 2)
         });
       }
     });
   };

})(jQuery);*/

$.fn.contentBorder = function(){
		var html = '';
		for (var i=0;i<8;i++){
			html += "<div class='content-border cb-"+i+"'></div>";
		}
		return this.append(html);
}

$.fn.truncate = function(){
		var elFirst = this.get(0);
		if (elFirst == null) return this;
		var $truncatees;
		var $elNext = $(elFirst).next(),$elNextNext;
		var index = 0,el;
		while (index++<100) {
			$elNext = $elNext.next();
			if ($elNext.hasClass("intro")) break;
			if ($elNext.hasClass("nav-pages")) break;
			$elNextNext = $elNext.next();
			if ($elNextNext.hasClass("nav-pages")) break;
			$truncatees = $truncatees ? $truncatees.add($elNext) : $elNext;
			
		}
		if (!$truncatees) return;
		
		$truncatees.wrapAll("<div style='position: relative; height:46px; overflow: hidden; margin-bottom: 15px'></div>");
		var $cont = $truncatees.parent();
		
		var elCont = $cont.get(0);
		if (elCont.offsetHeight!=elCont.scrollHeight) {
			var html = "<p style='position: absolute; right: 0px; bottom: 0px; background-color: #000; margin-bottom: 0px;'>... <a href='#untruncate' onclick='return untruncate(this)'>&raquo; Read whole text</a></p>";
			$cont.append(html);
		}
		
		return this;
}

function untruncate(el){
	var elP = el.offsetParent
	var elParent = elP.offsetParent;
	elParent.style.height = "";
	elParent.style.marginBottom = "";
	elParent.style.display = "";
	elP.style.display = "none";
}

$.fn.newsPages = function(itemsPerPage){
        if (this.length==0) return this;
        var $items = $("li",this);
        if ($items.length<itemsPerPage) return this;
		$items.hide();
        this.attr("itemsPerPage",itemsPerPage);
		var pcount = Math.ceil($items.length/itemsPerPage);
		//find visible range;
		
	    var html = "";
		for (var i=0;i<pcount;i++){
		    html += "<li><a href='#"+i+"'>"+(i+1)+"</a></li>";
		}
		this.after("<ul class='nav-news-pages'>"+html+"</ul>");
		var $pages = this.next().find("a");
		$pages.click(function(){
		   $(this).pageNewsPages();
		   return false;
		});
		
		$pages.eq(0).addClass("current").end();

		$items = $("li",this);
		var currId = $(".panel-body h1").attr("id").replace("newsItem-","");
		var currIndex = $items.index($("#newsItemLink-"+currId).parent("li").get(0));
		
		var currPage = Math.floor(currIndex/itemsPerPage);
		
		    $pages.eq(currPage).click();

		return this;
}

$.fn.pageNewsPages = function(){
    var el = this.get(0);
    var index = el.href.split("#")[1];
    var $list = $(this).parent().parent();
    $list.find("a").removeClass("current");
    this.addClass("current");
    $list = $list.prev();
    var itemsPerPage = parseInt($list.attr("itemsPerPage"));
    var start = itemsPerPage*index;
    var end = start + itemsPerPage;
    //var $items = $list.find("li");
    $list.find("li").hide().slice(start,end).fadeIn();
    return this;
}