$(document).ready(function(){
	// kolla on load om det är smartphone
	var useragent = navigator.userAgent;
	if( $(window).width() < 1024 || navigator.userAgent.indexOf("iPad") > -1 || navigator.userAgent.indexOf("iPhone") > -1 || navigator.userAgent.indexOf("Android") > -1){
		setIPhoneStyle();

		//sätt lyssnare för klick på bilden
		$('.bigVersion #imageHolder img').bind("click", function() {
		  	location.href = $("a.bigimage").attr("href");
		});	
		if( $(window).width() > 500 ) {
			//byt bilder för next och previous knapparna
			$("#next a img").attr("src", "/js/BigNext.png");
			$("#prev a img").attr("src", "/js/BigPrev.png");		
		}
	}
	// sätt lyssnare för resize
	var sizeSet = false;
	$(window).resize(function(){
		if( $(window).width() < 1024 && sizeSet == false && $("select").length == 0 ){
			setIPhoneStyle();
			sizeSet = true;
		}
		if( $(window).width() > 1024 && sizeSet == true ){
			$("select").remove();
			$("#submenu").show();
			sizeSet = false;
		}
	});
});

function setIPhoneStyle(){
	if($("#submenu").children().length > 1 && $("#submenu li.active").length == 0){
		$("#submenu").hide();

		$("#contentContainer div:first-child").append("<select/>");

		var title = $("h1").html();
		if( title == "PHOTOGRAPHERS" || title == "STYLISTS" || title == "HAIR/MAKEUP" || title == "ILLUSTRATORS" || title == "ANIMATION/FILM" )
			$("select").append("<option>CHOOSE ARTIST</option>");
		else {
			$("select").append("<option>CHOOSE</option>");
		}

		$("ul#submenu > li a").each(function(){
			if ( $(this).parent().parent().attr("id") == "submenu" ){
				$('select').append('<option value="' + $(this).attr("href") + '">' + $(this).html() + '</option>');
			}
		});

		// sätt lyssnare för val i dropdown
		$("select").change(function(){			
			var thisval = $(this).val();
			if(thisval != ""){
				location.href = thisval;
			}
		});	
	} else if($("#submenu").children().length > 1 && $("#submenu li.active").length > 0) {
		$("#submenu").hide();

		if(location.href.indexOf("contact") > -1 || location.href.indexOf("production") > -1 || location.href.indexOf("newsletter") > -1 || location.href.indexOf("facebook") > -1){
			// if contact for example
			$("#content").prepend("<select/>");
			$("select").append("<option>MORE INFORMATION</option>");
			$("ul#submenu > li a").each(function(){
				if ( $(this).parent().parent().attr("id") == "submenu" && $(this).parent().attr("id") != "contactInfo"){
					$('select').append('<option value="' + $(this).attr("href") + '">' + $(this).html() + '</option>');
				}
			});			
		} else {
			$("#contentContainer div:first-child").append("<select/>");
			if( $("h1").html() == "PHOTOGRAPHERS" )
				$("select").append("<option>CHOOSE</option>");
			else {
				$("select").append("<option>CHOOSE CATEGORY OR BIO</option>");
			}

			$("ul#submenu > li.active ul li a").each(function(){
				if ( $(this).parent().parent().parent().parent().attr("id") == "submenu" ){
					$('select').append('<option value="' + $(this).attr("href") + '">' + $(this).html() + '</option>');
				}		
			});			
		}

		// sätt lyssnare för val i dropdown
		$("select").change(function(){			
			var thisval = $(this).val();
			if(thisval != ""){
				location.href = thisval;
			}
		});			
	}
}

