// JavaScript Assests
var $j = jQuery;

// mycraousel Function
function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });
    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

// Url preview script written by Alen Grakalic 
 this.screenshotPreview = function(){	
		// CONFIG 
		xOffset = 1;
		yOffset = 3;
	// END CONFIG 
	$j("a.screenshot").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$j("body").append("<p id='screenshot'><img src='/images/img/"+ this.rel +"' alt='url preview' />"+ c +"</p>");								 
		$j("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$j("#screenshot").remove();
    });	
	$j("a.screenshot").mousemove(function(e){
		$j("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};


function bookmarkTheSite() {

	$j("a.jqbookmark").click(function(e){
		e.preventDefault(); // this will prevent the anchor tag from going the user off to the link
		var bookmarkUrl = this.href;
		var bookmarkTitle = this.title;
	 
		if (window.sidebar) { // For Mozilla Firefox Bookmark
			window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
		} else if( window.external || document.all) { // For IE Favorite
			window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
		} else if(window.opera) { // For Opera Browsers
			$j("a.jqbookmark").attr("href",bookmarkUrl);
			$j("a.jqbookmark").attr("title",bookmarkTitle);
			$j("a.jqbookmark").attr("rel","sidebar");
		} else { // for other browsers which does not support
			 alert('Your browser does not support this bookmark action');
			 return false;
		}
	});

}

function copyAddress() {
	if ($("input#BillingAddress").val() == "") {
		$("input#BillingAddress").val($("input#ShippingAddress").val()); 
	}
	if ($("input#BillingCity").val() == "") {
		$("input#BillingCity").val($("input#ShippingCity").val());
	}
	if ($("input#BillingState").val() == "") {
		$("input#BillingState").val($("input#ShippingState").val());
	}
	if ($("input#BillingZip").val() == "") {
		$("input#BillingZip").val($("input#ShippingZip").val());
	}
		var billIndex = $("#ShippingCountry option").index($("#ShippingCountry option:selected"))
		$("#BillingCountry option:eq("+billIndex+")").attr("selected", "selected")
}


function createAmountLocation () {
		
	$j("#CAT_Custom_69052_0").click(function()
	{
		var vamount = $j(this).val();
		$j("input#Amount").val(vamount); $j("input#subarea").val('United States & Canada : 1 Year Subscription');
	});
	$j("#CAT_Custom_69052_1").click(function()
	{
		var vamount = $j(this).val();
		$j("input#Amount").val(vamount); $j("input#subarea").val('United States & Canada : 2 Year Subscription');
	});
	$j("#CAT_Custom_69052_2").click(function()
	{
		var vamount = $j(this).val();
		$j("input#Amount").val(vamount); $j("input#subarea").val('Australia & Pacific Islands : 1 Year Subscription');
	});
	$j("#CAT_Custom_69052_3").click(function()
	{
		var vamount = $j(this).val();
		$j("input#Amount").val(vamount); $j("input#subarea").val('Australia & Pacific Islands : 2 Year Subscription');
	});
	$j("#CAT_Custom_69052_4").click(function()
	{
		var vamount = $j(this).val();
		$j("input#Amount").val(vamount); $j("input#subarea").val('New Zealand : 1 Year Subscription');
	});
	$j("#CAT_Custom_69052_5").click(function()
	{
		var vamount = $j(this).val();
		$j("input#Amount").val(vamount); $j("input#subarea").val('New Zealand : 2 Year Subscription');
	});
	$j("#CAT_Custom_69052_6").click(function()
	{
		var vamount = $j(this).val();
		$j("input#Amount").val(vamount); $j("input#subarea").val('UK, Europe & Rest of World : 1 Year Subscription');
	});
	$j("#CAT_Custom_69052_7").click(function()
	{
		var vamount = $j(this).val();
		$j("input#Amount").val(vamount); $j("input#subarea").val('UK, Europe & Rest of World : 2 Year Subscription');
	});
	
}


// Billing same as shipping?
function billToShip()
{
	if ( $j("input[@name='bill_q']:checked").val() == "Yes" ) {
		$j("span.optional").show();
	} else {
		$j('span.optional').hide();
		copyAddress();
	}
	
	$j("#bill_q_0").change(function()
	{
		if ( $j(this).val() == "Yes" ) {
			$j("span.optional").slideDown('slow');
		}
	});
	$j("#bill_q_1").change(function()
	{
		if ( $j(this).val() == "No" ) {
			$j("span.optional").slideUp('fast');
			copyAddress();
		}
	});

}


function subDropdown() {
	$j('#subscribelist').hover(
    	function () {
            //show its submenu
        	$j('ul', this).slideDown(100);
        	$j(this).addClass("overme");
 
        },
        function () {
            //hide its submenu
            $j('ul', this).slideUp(100); 
            $j(this).removeClass("overme");       
        }
    );
}

/* Dom Load */
$j(function() { 
	/* Image Preloader */
	$j.preloadImages = function()
	{
	  for(var i = 0; i<arguments.length; i++)
	  {		$j("<img>").attr("src", arguments[i]);	  }
	}
	$j.preloadImages("../images/bg/h-menu-hover-active.png");
	
	
	
	
	function showTwitteFeed() {
	
		var targetID = "";
		
		if ( $j(".twitterFeed").length ) {		
			targetID = $j(".twitterFeed").attr("id");
		}
		
			switch (targetID)
			{
			case "helopsTwitterFeed":
				$j('#helopsTwitterFeed').liveTwitter('HeliOpsMagazine', {limit: 3, refresh: false, mode: 'user_timeline'});	  
				break;
			case "p1magTwitterFeed":
			  $j('#p1magTwitterFeed').liveTwitter('P1Magazine', {limit: 3, refresh: false, mode: 'user_timeline'});	
			  break;
			case "clearedhotTwitterFeed":
			  $j('#clearedhotTwitterFeed').liveTwitter('militaryhelos', {limit: 3, refresh: false, mode: 'user_timeline'});	
			  break;
			default:
				$j('#helopsTwitterFeed').liveTwitter('HeliOpsMagazine', {limit: 3, refresh: false, mode: 'user_timeline'});	
			}


	
	}
	
	

	
	$j('#helopsTwitterFeed').liveTwitter('HeliOpsMagazine', {limit: 3, refresh: false, mode: 'user_timeline'});
	
	$j('#forum-rules').hide();
	// Drop down slider
	/* $j('#subscribelist').droppy(); */
	subDropdown();
	// News ticker
	$j("#news").newsticker();
	// Carousel
	$j('#mycarousel').jcarousel({ auto: 3, wrap: 'both', animation: "slow", speed: 400, initCallback: mycarousel_initCallback });
	// Contact us accordian 
/*	$j("#contact-accordion").accordion({ collapsible: true, autoHeight: true });*/
	// Email Converter
	$j('a.email').each(function(){
			e = this.rel.replace('/','@');
			this.href = 'mailto:' + e; $j(this).text(e);
	});
	
	screenshotPreview();
    $j('a#rules-toggle').click(function() {
    	$('#forum-rules').toggle(200); return false;
  	});	
  	
  	$j('.subscribe').hover(function(){
  		$j(this).addClass("overme");
  	});
  	if ( $j('.subscribe').hasClass("overme") ) {
  		$j('#dropdownsubs').mouseleave(function(){
  			$j('.subscribe').removeClass("overme");
  		});
  	}
  	
  	$j('.subscribe').mouseleave(function(){
  		$j('.subscribe').removeClass("overme");
  	});
  	$j('#dropdownsubs').hover(function(){
  		$j('.subscribe').addClass("overme");
  	});
  	
  	if ( $j("#wallpaper-links").length ) {
  		$j("li.wallpaper:nth-child(3n)").addClass("end-right");
  	}
  	
	billToShip();
	bookmarkTheSite();
	createAmountLocation();
	showTwitteFeed();
	
});





