// Here for iframe call

function resizeIframe(h, t) {

	//var f=document.getElementById('shopServerBlock');

	//if(f) f.style.height = f.contentWindow.document.body.scrollHeight + 'px';

	//var $frame = jQuery('#shopServerBlock');

	//if(h == undefined) h = 6800;

	//$frame.attr('height', h);

	//if($frame.length){

		//$frame.css('height', f.contentWindow.document.body.scrollHeight + 'px');

		//f.style.height = f.contentWindow.document.body.scrollHeight + 'px'

		//alert('resize 1 - ' + f.contentWindow.document.body.scrollHeight);

	//}

	$('#shopServerBlock').attr('height', h);

	document.title = t;

};



	//Image roller for frontpage image

	var numImgs = 11;

	var timeInt = 8;

	function switchImg(){

  // @todo: remove! 
  

		var imgId = $('#image-roller img').attr('src').slice(-6).replace('/', '').replace('.jpg', '');

		



		if(imgId < numImgs){

			imgId ++;

		}else{

			imgId = 1;

		}



		var url = '/shopserver/images/random/norway/forside/'+imgId + '.jpg';

		$('<img />').attr('src', url).load(function(){

			$('#image-roller img').fadeOut(function(){

				$(this).attr('src', url).fadeIn();

				setTimeout('switchImg();', timeInt*1000);

			});

		});

	}

	setTimeout('switchImg();', timeInt*1000);



(function($){

	// Resizing iframe for shopServer

	/*function resizeIframe() {

		var f=document.getElementById('shopServerBlock');

		//if(f) f.style.height = f.contentWindow.document.body.scrollHeight + 'px';

		var $frame = $('#shopServerBlock');

		//if($frame.length){

			$frame.css('height', f.contentWindow.document.body.scrollHeight + 'px');

		//}

		

		alert('resize 2');

	}*/





	function createFrame(){

		var $frame = $('<iframe id="shopServerBlock" name="shopServerBlock" />');

		$frame.attr({ src : '', width:'800', scrolling:'no', frameBorder:'0', border:'0'});

		$('#mainContent').hide().after($frame);

		//document.getElementById('shopServerBlock').onload = resizeIframe;

		//window.onresize = resizeIframe;

		//document.getElementById('shopServerBlock').style.border = 'none';

		//document.getElementById('shopServerBlock').onload = resizeIframe;

		//frames["shopServerBlock"].onload = resizeIframe;

		//window.onresize = resizeIframe;

		//console.log('in createFrame()');

		$('#frontfeatures, #Table12').hide();

		var prevHeight = 0;

		/*setInterval(function(){

			if(window.location.hash == '' || window.location.hash == '#'){

				return;

			}else{

				//if(window.location.hash != prevHeight){

					document.getElementById('shopServerBlock').style.height = window.location.hash.substring(1) +"px";

					window.location.hash = '#';

				//}

			}

		}, 100);*/

	}



	$(document).ready(function(){



		// Start with the random frontpage image

		var rand=Math.ceil(Math.random()*11);

		$('#image-roller img').attr('src', '/shopserver/images/random/norway/forside/'+rand+'.jpg');

	

		$('#block-menu-primary-links li.expanded').hover(function(){

			$('ul', this).show();

		}, function(){

			$('ul', this).hide();

		});

		$('#block-menu-primary-links li.expanded ul li').hover(function(){

			$(this).css('background-color', '#BBC591');

		}, function(){

			$(this).css('background-color', '#fff');

		});



		// Change iframe src on search category change

		$('#DropdownAllCategories').change(function(){

			if($('#shopServerBlock').length == 0){

				createFrame();

			}

			document.getElementById('shopServerBlock').src = $(this).val();

		});



		// Search button click event

		$('#ButtonSearch').click(function(){

			if(document.getElementById('shopServerBlock') == null){

				createFrame();

			}

			if($('#TextBoxSearchWords').val() != ''){

				//document.getElementById('shopServerBlock').src =  $('#DropdownAllCategories :selected').val() + '&Search='+$('#TextBoxSearchWords').val();

				document.getElementById('shopServerBlock').src = '/shopserver/index.aspx?type=ProductCollection&Lang=norway&Search='+$('#TextBoxSearchWords').val();

			}

			return false;

		});



		// Menu items to control iframe url

		$('#block-menu-primary-links a, a.iframe, #block-menu-menu-shopserver li a').click(function(e){

			e.preventDefault();

			if($('#shopServerBlock').length == 0){

				var $frame = $('<iframe id="shopServerBlock" name="shopServerBlock" />');

				$frame.attr({ src : $(this).attr('href'), width:'800', scrolling:'no', frameBorder:'0', border:'0'});

				$('#mainContent').hide().after($frame);

				//document.getElementById('shopServerBlock').onload = resizeIframe;

				//window.onresize = resizeIframe;

				$('#frontfeatures, #Table12').hide();

			}else{

				document.getElementById('shopServerBlock').src = $(this).attr('href');

			}

			//document.getElementById('shopServerBlock').src = $(this).attr('href');

			return false;

		});

		

	});

})(jQuery);




