/* Lover ® */

var ipad = navigator.userAgent.match(/iPad/i);
var iphone = navigator.userAgent.match(/iPhone/i);
var ipod = navigator.userAgent.match(/iPod/i);

$(document).ready(function(){
						   
//Fade Ins
/*if( $("div.content").hasClass("press") ||
	$("div.content").hasClass("contacts") ||
	$("div.content").hasClass("stockists") ||
	$("div.content").hasClass("grid") || 
	$("div.content").hasClass("login") || 
	$("div.content").hasClass("cart")||
	$("div.content").hasClass("left")||
	$("div.content").hasClass("films")||
	$("div.content").hasClass("gallery")
	){
	
	$("section article").hide().css({"opacity" : "0"});
	$("section article").show().animate({"opacity" : "1"}, 1000);

}*/

if( $("section article div").hasClass("slider_profile") || $("section article div").hasClass("slider") ){
		$("section article").css({"opacity" : "0"});
		
	$("section article div").find("img").eq(0).load(function(){
		$("section article").show().animate({"opacity" : "1"}, 1000);
	}).each(
	function(){
		if( this.complete ){
			$("section article").show().animate({"opacity" : "1"}, 1000);
		}
	});
	

}else if( $("section").hasClass("press") ){
	
		$("section article").css({"opacity" : "0"});
	$("section article").find("img").eq(0).load(function(){
		$("section article").show().animate({"opacity" : "1"}, 1000);	
	}).each(
	function(){
		if( this.complete ){
			$("section article").show().animate({"opacity" : "1"}, 1000);
		}
	});
	
}else if( $.trim( $("section article").html() ).indexOf('<a href="javascript:history.back();"><img src=') == 0){
/*}else if($("section article").hasClass("press") ){*/

	
	$("section article").find("img").css({"cursor" : "pointer"});
	$("section article").find("img").eq(0).load(function(){												 
		$("section article").show().animate({"opacity" : "1"}, 1000);
	}).each(
	function(){
		if( this.complete ){
			$("section article").show().animate({"opacity" : "1"}, 1000);
		}
	});
	
	
}else{

	$("section article").hide().css({"opacity" : "0"});
	$("section article").show().animate({"opacity" : "1"}, 1000);

	
}

//						   

	initGallery();
	
	
/*	
	$(window).bind('orientationchange', function(event){
												
	event.preventDefault();	
	if(window.orientation === 0 || window.orientation === 180){
		$("body").hide();
	}else{
		$("body").show();
	}
		
	});
	
*/	
	

//FIXES
//Last Children for IE
$(".content div:last-child").css({"margin-bottom" : "0px"});
$(".content.right div:last-child").find("span").css({"padding-bottom": "3px !important"});

//Border-radius for IE
if (typeof(document.body.style.borderRadius) === "undefined") {
	$("#purchase-button").css({"background" : "url(images/purchase_2.png) no-repeat rgb(210, 210, 210)"});
	$("#purchase-button").bind("mouseover mouseout", function(e){
	
		if (e.type === "mouseover"){
		$(this).css({"background" : "url(images/purchase_2.png) no-repeat rgb(190, 190, 190)"});	
		}
		
		if (e.type === "mouseout"){
		$(this).css({"background" : "url(images/purchase_2.png) no-repeat rgb(210, 210, 210)"});	
		}
		
	});
}

//	

// mouse over grey bar functions //

	
	$(".grid div").bind("mouseover mouseout", function(e){
		
			if (e.type == "mouseover"){
				$(this).find("div").css({"background" : "rgb(226, 226, 226)"});
			}
			
			if (e.type == "mouseout"){
				$(this).find("div").css({"background" : "rgb(236, 236, 236)"});
			}

	});
	
	$(".row").bind("mouseover mouseout", function(e){
		
			if ( $(this).hasClass("heading") || $(this).parent().hasClass("login")){
				return false;
			}else{
		
				if (e.type == "mouseover"){
					$(this).find("div").css({"background" : "rgb(226, 226, 226)"});
				}
				
				if (e.type == "mouseout"){
					$(this).find("div").css({"background" : "rgb(236, 236, 236)"});
				}
			}
			
			if ( $(this).parent().hasClass("press") || $(this).parent().hasClass("films") || $(this).parent().hasClass("collections")){
				$(this).css({"cursor" : "pointer"});
			}

	});
	
	$("#popup-window").find("hgroup").bind("mouseover mouseout click", function(e){

				if (e.type == "mouseover"){
					$(this).css({"background" : "rgb(226, 226, 226) !important"});
				}
				
				if (e.type == "mouseout"){
					$(this).css({"background" : "rgb(236, 236, 236) !important"});
				}
				
				if (e.type == "click"){
					$("#popup").hide();

					$("#popup-background").css({"opacity" : "0"});
				}				

	});
	
	$("#popup").bind("click", function(e){

				if (e.type == "click"){
					//$("#popup").hide();

					//$("#popup-background").css({"opacity" : "0"});
				}				

	});
	
$(window).resize(function(){
	
	resizePopup();
				 
});	
						 
	
// mouse over grey bar functions //	
	

/*	
$(window).scroll(function(){
	
	if ( $("footer").offset().top - parseInt( $("#top").css("padding-top") ) - $(window).scrollTop() <= 502){
		
		if ( $("#top").css("position") != "absolute"){
		$("#top").css({"position" : "absolute"});
		$("#top").css({"top" : $("footer").offset().top - 607 + "px" });
		

		}
		

	}else if( $("footer").offset().top - parseInt( $("#top").css("padding-top") ) - $(window).scrollTop() >= 502){
		
		$("#top").css({"position" : "fixed"});
		$("#top").css({"top" : "0px" });
		
	}

						  
});	
*/


 
 

//popup window

$("#popup").hide();

$("#popup-background").css({"opacity" : "0"});

$(".pop_btn").click(function(){
	data = $(this).attr('data');							 
	$.post("includes/aux_page.php", { aux_page: data },
	function(data) {
		$("#popup .t").html(data.title);
		$("#popup .content").html(data.copy);
		resizePopup();
		$("#popup").show();
		$("#popup-background").animate({"opacity" : ".8"}, 500);
		
		if(ipad != null || iphone != null || ipod != null){
			new iScroll('apple', {desktopCompatibility:true});
		}
		
	}, "json");
});
$(".pop_join_btn").click(function(){
	
	$("#popup .content").load('inc/joinus.php', function() {
		$("#popup .t").html('Join the Black Rose Army');
		resizePopup();
		$("#popup").show();
		$("#popup-background").animate({"opacity" : ".8"}, 500);
		
		if(ipad != null || iphone != null || ipod != null){
			new iScroll('apple', {desktopCompatibility:true});
		}
		
	});
	
	
	/*
	$.post("inc/joinus.php", { aux_page: data },
	function(data) {
		$("#popup .t").html(data.title);
		$("#popup .content").html(data.copy);
		resizePopup();
		$("#popup").show();
		$("#popup-background").animate({"opacity" : ".8"}, 500);
	}, "json");
	*/
});

//



//Choose country
down = false
$(".select-country").click(function(){
																	
	if (down == false){
	$(".select-country").find("img").attr("src", "images/uparrow.gif");
	$("#select-country").show();
	down = true;
	}else{
	$(".select-country").find("img").attr("src", "images/downarrow.gif");
	$("#select-country").hide();	
	down = false;
	}
						   
});

$("#select-country ol li a").bind("click", function(){
	$(".select-country").find("img").attr("src", "images/downarrow.gif");
    $("#select-country").hide();    
    down = false;					  
});

$(".select-country").bind("selectstart", function(){return false});

	
						 
	
}); //// document ready ------------------------------------------------------------------------------------------------------------------------------


function stockists(){
	
	country = $(this).attr("id");
	scrollFromTop = $(".content").offset().top;
	scrollto = $(document).find("a[name='" + country + "']").offset().top - scrollFromTop;
	
	//$(window).scrollTop( $(document).find("a[name='" + country + "']").offset().top - scrollFromTop);	

	$("html, body").stop().animate({ scrollTop: scrollto}, 800);
	
	

	
}





function initGallery(){

// #1 // Variables //		
	
totalImages = $("#view").find("img").length;
node = 0;
count = node + 1;	

$(".nav-bar.buy a").attr('href', 'item'+$("#view").find("img").attr('data'));

$("a.b-left").attr('href', $("#view").find("img").attr('look')+'/1');
$("a.b-right").attr('href', $("#view").find("img").attr('look')+'/2');

if($("#view").find("img").attr('data')) $(".nav-bar.buy").animate({"bottom" : "23px"}, 300);


// #3 // Hide Images except 0 & fade in image-gallery//
										   

$("#view").find("img").each(function(i){
	$(this).hide();
	$(this).css({"opacity" : "0"});
	if ($(this).width() < 734){
		$(this).css({"left": "50%", "margin-left" : $(this).width() / 2 * -1 + "px"});
	}
});


// #4 // Update progress text with total images and current image //

if (totalImages == 0){totalImages = 1;}
$("#figure span").text(count + "/" + totalImages);
	
} // initGallery ------------------------------------------------------






function resizePopup(){
	
		if( $(window).height() < $("footer").offset().top + $("footer").height() + parseInt($("footer").css("margin-bottom")) ){
			$("#popup").height( $(document).height() );
		}else{
			$("#popup").css({"height" : "100%"});
			
		}
	
}
