// JavaScript Document

// SITES MAIN ONLOAD FUNCTION
$(function() {
	/// CUSTOM SCROLL BARS 
//	$('.scroll-pane').jScrollPane({
//		showArrows:true,
//		scrollbarWidth : 12,
//		arrowSize : 15,
//		reinitialiseOnImageLoad : true
//	});
	//
	// IMAGE PRE-LOADER
	$.preloadImages("/layout/ON_on.png", "/layout/SK_on.png","/layout/MB_on.png","/layout/leftnav_over.png","/layout/topbutton_over_bg.png");
	//
	// If Highlight Box
	$('.highlight_content').find('div.promo_item').each( function(index,el) { 
									if ( index == 0 ) { $(el).show();
									} else { $(el).hide(); }
								} );
	setTimeout("rotate_highlight('');",5000);
	//
	// ADDITIONAL BUTTON EFFECTS
	$('.ui-state-default').bind('mouseover', function() { $(this).addClass('ui-state-hover'); } );
	$('.ui-state-default').bind('mouseout', function() { $(this).removeClass('ui-state-hover'); } );
	//
	// SEARCH WIDGET INIT
	$('#searchWidget').hide();
	$("#search_trigger").bind( 'click', function() { 
		$("#searchWidget").animate( { "height": "toggle", "opacity": "toggle" }, "slow" );
	} );
	$("#searchWidget input[type='text']").bind( 'focus', function(){ if ( $(this).val() == 'Type Your Search Here ...' ) { $(this).val(""); $(this).removeClass('dim'); } } );
	$("#searchWidget input[type='text']").bind( 'blur', function(){ if ( $(this).val() == '' ) { $(this).val("Type Your Search Here ..."); $(this).addClass('dim'); } } );
	// ADJUST PAGE SCROLL
	
	$(".marquee").each(function(){ $(this).myMarquee(); });
	$('.dPicker').datepicker({  
		dateFormat: 'yy-mm-dd' 
	 });  
} );

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

	$.fn.extend({
		myMarquee: function(settings) {
			var obj = this;
			var intervalId = false;
			// default config 
			var defaults = jQuery.extend({
				slideLength: 133,
				fadeType: 'swing',
				itemClass : 'highlight',
				children : 1
			},settings);

			var options = $.extend(defaults, options);
			
			this.flipBack = function() {
				obj.find('.prev').unbind('click');
				var curr = parseInt(obj.find(".sectionMarquee").css('left').replace("px",""));
				var index = Math.abs((curr-30) / options.slideLength);
				if ( index <= 0 ) {
				//	console.log( "Curr:"+curr+". Index:"+index+". Length:"+options.slideLength+". Children:"+options.children+". Move TO:"+30-(options.slideLength*(options.children-3))+"px" );
					obj.find(".sectionMarquee").animate({left:30-(options.slideLength*(options.children-5))+"px"});
				} else {
					//console.log( (curr+options.slideLength)+"px 2" );
					obj.find(".sectionMarquee").animate({left:(curr+options.slideLength)+"px"});
				}
				obj.find('.prev').bind('click',function() { obj.flipBack(); } );
			}
			this.flipAhead = function() {
				obj.find('.next').unbind('click');
				var curr = parseInt(obj.find(".sectionMarquee").css('left').replace("px",""));
				var index = Math.abs((curr-30) / options.slideLength);
				//console.log( index );
				if ( index >= ( options.children - 5 ) ) {
					obj.find(".sectionMarquee").animate({left:"30px"});
				} else {
					obj.find(".sectionMarquee").animate({left:(curr-options.slideLength)+"px"});
				}
				obj.find('.next').bind('click',function() { obj.flipAhead(); } );
			}
			/// INIT FUNCTION
			return this.each(function () {
				options.children = obj.find("div.productSectionPhoneContainer").length;
				obj.find(".sectionMarquee").css('width',( options.slideLength * options.children ));
				//console.log( options.slideLength + '*' + options.children );
				obj.find('.prev').bind('click',function() { obj.flipBack(); } );
				obj.find('.next').bind('click',function() { obj.flipAhead(); } );
			});
		}
	} );

///////////////////////////////////////

function redir( href ) {
	window.location.href = "/"+href;	
}

function rotate_highlight( id ) {
	if ( id ) {
		$('.promo_item').hide();
		$('#promo_item_'+id).show();
		$('#highlight_on').val('0');
	} else if ( $('#highlight_on').val() == '1' ) {
		var curr = $('#highlight_current').val();
		var count = $('.promo_item').length - 1;
		if ( curr == count ) curr = 0;
		else curr++;
		$('.promo_item').fadeOut();
		$('#promo_item_'+curr).fadeIn();
		$('#highlight_current').val( curr );
		setTimeout("rotate_highlight('');",3600);
	}
}

///////////////////////////////////////
// SHOPPING CART FUNCTIONS
///////////////////////////////////////

function setAccessory( accid, itemid ) {
	$.ajax( {
		  url : 'ajax_admin.php',
		  type : 'post',
		  data : "AJAXaction=toggleAccessory&aid="+accid+"&pid="+itemid,
		  success : function( msg ) {
			  reset_billing()
		  }
		  } );
}

function setContract(el,pid) {
	var contract = el.options[el.options.selectedIndex].value;
	$.ajax ( {
		  url : 'ajax_admin.php',
		  type : 'post',
		  data : "AJAXaction=setContract&contract="+contract+"&pid="+pid,
		  success : function( msg ) {
			   reset_billing();
		  }
	   } );
}
///////////////////////////////////////
// POPUP FUNCTIONS
///////////////////////////////////////
var win;
function show_popup( content, title, callback ) {
	
	var newdiv = document.createElement( 'div' );
	newdiv.id = 'dialog';
	
	document.body.appendChild( newdiv );
	var innerdiv = document.createElement( 'div' );
	innerdiv.id = 'popup_content';
	newdiv.appendChild( innerdiv );
	
	innerdiv.innerHTML = content;
	
	var btns;
	if (callback != 'NONE') { 
		if (callback != 'NULL') { 
			btns = { 
				"Ok": function() { 
					eval( callback ); 
					destroy_popup(); 
				},  
				"Cancel" : function() { 
					destroy_popup(); 
				} 
			};
		} else {
			btns = { 
				"Cancel" : function() { 
					destroy_popup(); 
				} 
			};
		}
	}
	$("#dialog").dialog({
		bgiframe: true,
		width: 670,
		height: 550,
		modal: true,
		title: title,
		buttons: btns,
		close: function() {
			destroy_popup();
		}
	});
}

function destroy_popup() {
	$('#dialog').dialog('destroy');
	$('#dialog').remove();
}


function demoMap( lat, long, name ){
	//<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true_or_false&amp;key=ABQIAAAABmk4fQYza2WJMigJW5WnBBRPcMjDUGY-8KWEnUwTod_KkQt2vRSQqEWfbYVT8zBq4KAFPZQkkfEKuA" type="text/javascript"></script>

 Shadowbox.open({
	 player: 'html',
	 content: '',
	 height: 400,
	 width: 600,
	 options: {
	 onFinish: function(item){
			 if(GBrowserIsCompatible()){
				var map = new GMap2(document.getElementById('sb-content'));
				var center = new GLatLng(parseFloat(lat),parseFloat(long));
				map.setCenter(center, 14);
				map.enableScrollWheelZoom();
	//			map.addControl(new GLargeMapControl3D());
				
				map.addMapType(G_PHYSICAL_MAP);
				var hControl = new GHierarchicalMapTypeControl();
				hControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Labels", false);
				map.addControl(hControl);
				
				var marker = new GMarker(center);
				marker.openInfoWindowHtml(name);
				map.addOverlay(marker);
				
//				 map.openInfoWindow(map.getCenter(), document.createTextNode(name));
			 }
			 // add some simple controls
			 map.addControl(new GSmallMapControl());
			 map.addControl(new GMapTypeControl());
		 }
	}
 });

};

function showAccessory( id ) {
	
	Shadowbox.open({
			content:    '<div style="text-align:center;margin-top:200px;"><img src="/layout/ajax-loader2.gif" /></div>',
			player:     "html",
			title:      "",
			height:     450,
			width:      750
		});
	$.ajax( { 
		  url : "/ajax_admin.php",
		  type : "post",
		  dataType : 'json',
		  data : "AJAXaction=getAccessory&id="+id,
		  success: function( data ) {
			  Shadowbox.getCurrent().title = data.title;
			  Shadowbox.getCurrent().content = data.content
		  }
	} );
}

function submitContactForm() {
	var name = $("#FullName").val();	
	var experience = $("#ExperienceAt").val();
	var feedback = $("#Feedback").val();
	$("#btnContainer").html( "<center><img src='/layout/ajax-loader2.gif' /></center>" );
	$.ajax( {
		  url : '/ajax_admin.php',
		  type : 'post',
		  data : "AJAXaction=sendFeedback&name="+name+"&experience="+experience+"&feedback="+feedback,
		  success : function( msg ) {
			  if ( msg == 'Sent!' ) {
				  $("#btnContainer").html( "Message Sent. Thank you." );
			  } else {
				  
			  }
		  }
		} );
}
