// sIFR - Flash text replacement
function init_sifr() {
	if(typeof sIFR == "function"){
		sIFR.replaceElement(named({
			sSelector:"#header h2",
			sFlashSrc:"/js/oporto.swf", 
			sColor:"#000000",
			sWmode: "transparent",
			sCase: "upper"
		}));

		sIFR.replaceElement(named({
			sSelector:"#pageInfo h2, .s-home h2",
			sFlashSrc:"/js/oporto.swf", 
			sColor:"#000000",
			sWmode: "transparent",
			sCase: "upper"
		}));
		
		sIFR.replaceElement(named({
			sSelector:".s-home #content h3.sifr",
			sFlashSrc:"/js/oporto.swf", 
			sColor:"#424242",
			sWmode: "transparent",
			sCase: "upper"
		}));

		sIFR.replaceElement(named({
			sSelector:".popup h1",
			sFlashSrc:"/js/oporto.swf", 
			sColor:"#FFFFFF",
			sWmode: "transparent",
			sCase: "upper"
		}));		
	};
}
$(function(){
	if(typeof sIFR == "function" && !sIFR.UA.bIsIEMac && (!sIFR.UA.bIsWebKit || sIFR.UA.nWebKitVersion >= 100)){
		sIFR.setup();
		//sIFR.debug();
		if($("body").hasClass("popup")) { setTimeout(function(){init_sifr()},200); }
		else { init_sifr(); }
	};
});



// Thumbnail list (Rows of 4)
$(document).ready(function(){
	var row = 4;
	$(".split.s-3_4 .thumblist li").each(function(i){
		if (i == row - 1) {
			$(this).addClass("last").next().addClass("first");
			row = row + 4;
		}
	});
});


// Item previews
$(document).ready(function(){
	var row = 2;
	$(".itempreview").each(function(i){
		if (i == row - 1) {
			$(this).addClass("alt");
			row = row + 2;
		}
	});
});




/* Sidenav stuff
***** WAS READING FROM URL, NOW READING FROM HEADING, BELOW. ****
function getFileName(place) {
var url = place,
i = url.lastIndexOf('/') + 1,
j = url.indexOf('#', i),
k = url.indexOf('?', i);

if(-1 == j) {j = url.length;}
if(-1 == k) {k = url.length;}

return url.substring(i, Math.min(j, k));
}

$(document).ready( function() {
   var myurl = getFileName(document.URL);
   $('#localNav li a').each( function(i) {
	    var itemhref = $(this).attr("href");
		var convertedhref = getFileName(itemhref);
		if (convertedhref == myurl) {
			$(this).parent().addClass("active");
		}
	});
});
*/



// Sidenav Stuff

$(document).ready(function(){
	$('#localNav li a').each( function(i) {
	    var itemname = $(this).text();
		var heading = $("h2","#pageInfo").text();
		if (itemname == heading) {
			$(this).parent().addClass("active");
		}
	});
	$("#localNav li:first-child").addClass("first");
	$("#localNav li.active").prev().addClass("before");
});


// Drop Down Nav

$(document).ready(function(){

	$("ul.subnav").prev().addClass("drop");
	$("ul.subnav li").each(function(){
		if( $(this).html() == "" ) $(this).remove();
	});
	$("ul.subnav li:last-child").addClass("last");

	$("ul#globalNav a.drop").click(function() { //When trigger is clicked...
		
		$(this).addClass("dropped");

		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

		$(this).parent().hover(function() {
		}, function(){
			$(this).parent().find("ul.subnav").slideUp(500); //When the mouse hovers out of the subnav, move it back up
			setTimeout(function(){ $("a.drop").removeClass("dropped"); }, 500);	
		});
		
		return false; 

		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() {
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});

});


// Expanding sections
$(document).ready(function(){
	$(".hidden").hide();
	$("a.expand").click(function(){
		$(this).parent().next(".hidden").slideDown();
	});
	
	$(".form h3.active").next(".hidden").show();
	if($(".form .hidden").size() > 0)
	$(".form h3").css("cursor","pointer").click(function(){
			if ($(this).hasClass("active")) { $(this).removeClass("active"); }
			else { $(this).addClass("active"); }
			$(this).next().slideToggle(); 
			
	});

	$("h3 + .hidden").prev().addClass("arrow");
});

// 'FancyBox' calls. (Pretty new lightbox)
$(document).ready(function(){
	$("a.zoom").fancybox();
	$("a.storeselect").fancybox({
		frameWidth: 300,
		frameHeight: 300,
		padding: 2,
		overlayOpacity: 0.9,
		iframe: true,
		callbackOnShow: function(){
			init_sifr();
			$("#fancy_title").hide();
		},
		callbackOnClose: function(){
		   //window.location.reload();// - this does not work with IE
		   window.location = window.document.location;		
		}
	});
});

function closeFancybox() {
   var closebox = $('#fancy_close');
   if (closebox.length == 1)
      $(closebox).trigger('click');
}

$(document).ready(function(){
	if ( $("body").hasClass("popup") ){ }
	else { $("#fancy_outer").draggable(); }
});


// Rows - Applying literal style
$(document).ready(function(){
	// Checks cells for '<b>' elements and applies 'lit' class to parent span.
	$(".form table td > span").each(function(){
		var inputLiteral = $(this).find("b");
		if( inputLiteral.size() > 0 ) {
			inputLiteral.parent().addClass("lit");
			// Hide if value is empty.
			//if ( inputLiteral.text() == "" ) { inputLiteral.parent().hide(); }
		}
	});
	
});


$(document).ready(function(){
	$("span[id]",".form").each(function(){
		var span = $(this);
		var spanid = span.attr("id");
		if (spanid != "spanStore")
		{span.addClass("datecontrol").find("input:first").css("width","106px");}
	});
});


function StoreNameChanged(textBoxID, listBoxID) {
   var _textbox	= document.getElementById(textBoxID);
   var _listbox   = document.getElementById(listBoxID);
   var _founditem = false;
   if (_textbox != null && _listbox != null) {
      var _storename_length = _textbox.value.length;
	   var _storename_starts = _textbox.value.toLowerCase();
	   if (_storename_length > 0) {         
		   for (i=0;i<_listbox.length;i++) {
		      if (_founditem == false) {
		         var list_item_text = _listbox.options[i].text.toLowerCase();
               var list_item_text_trucated = list_item_text.substr(0, _storename_length);
		         if (list_item_text_trucated==_storename_starts) {
		            _listbox.selectedIndex = i;
		            _founditem = true;
		         }		         
		      }
		   }
	   }  
	}   
}	   
	
