/*
 * author: Tomasz Bińkowski
 * http://e-madness.pl/
 */
 
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

$(function() {
	
	$('#logo').click(function(){
		document.location.href = "/";
	});

	$('#show_hide_boxes').click(function(){
		if($('.box_img').css('display') == 'none') {
			$('#show_hide_boxes').css('background', 'url(/public_html/application/main/images/zwin.png)');
			$('#show_hide_boxes').css('background-repeat','no-repeat');
			$('#show_hide_boxes').css('background-position','top right');
			$('.box_img').show();
			$('.box_a').css('color', '#b4b4b4');
		} else {
			$('.box_img').hide();			
			$('#show_hide_boxes').css('background', 'url(/public_html/application/main/images/rozwin.png)');
			$('#show_hide_boxes').css('background-repeat','no-repeat');
			$('#show_hide_boxes').css('background-position','top right');
			$('.box_a').css('color', '#000000');
		}
	});

	$('#show-list').click(function(){
		if($('#product-attributes-list').height() == 350) {
			$('#product-attributes-list').height('auto');
			$('#show-list').html('Zwiń listę');
		} else {
			$('#product-attributes-list').height(350);
			$('#show-list').html('Rozwiń listę');
		}
	})

	if($('#product-attributes-list-holder').height()) {
		$("#attributes-slider-content").slider({
			orientation: "vertical",
			range: "min",
			value: 100,
			max: 100,
			min: 0,
			animate: true,
			change: handleSliderChange,
			slide: handleSliderSlide
		});
	}


	$('a[name=modal_image]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();

		//Get the A tag
		var id = '#dialog';

		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();

		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});

		//transition effect
		$('#mask').fadeIn(1000);
		$('#mask').fadeTo("slow",0.8);

		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();

		//Set the popup window to center
		$(id).css('top',  $(this).position().top-$(id).height());
		$(id).css('left', winW/2-$(id).width()/2);

		var html = '<img src="' + $(this).attr('href')+ '" >';
		$('#content-dialog').html(html);
		//transition effect
		$(id).show();
	});

	$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();

		//Get the A tag
		var id = '#dialog';

		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();

		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});

		//transition effect
		$('#mask').fadeIn(1000);
		$('#mask').fadeTo("slow",0.8);
		
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();

		//Set the popup window to center
		$(id).css('top',  $(this).position().top-$(id).height());
		$(id).css('left', winW/2-$(id).width()/2);

		var html = '<script type="text/javascript">';
		html += 'var params = { menu: "false", scale: "noScale", allowFullscreen: "true", allowScriptAccess: "always", bgcolor: "#FFFFFF"};';
		html += 'swfobject.embedSWF("/public_html/application/main/flash/player.swf", "altContent", "580", "375", "9.0.0", "expressInstall.swf", {movieSrc: "../image/video/'+ $(this).attr('href')+'", movieName: "'+ $(this).attr('class')+'", closeURL: "javascript:closeMyWindow()"}, params, {"bgcolor": "#333333", play: "true",loop:"true"});';
		html += '</script>';
		html += '<div id="altContent"><p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>';
		html +=	'</div>';

		$('#content-dialog').html(html);
		
		//transition effect
		$(id).show();

	});

	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();

		$('#mask').hide();
		$('.window').hide();
	});

	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});	
});

function  handleSliderChange(e, ui)
{
  var maxScroll = $("#product-attributes-list").attr("scrollHeight") -
                  $("#product-attributes-list").height();
  $("#product-attributes-list").animate({scrollTop:  (100-ui.value) *
     (maxScroll / 100)}, 1000);
}

function handleSliderSlide(e, ui)
{
  var maxScroll = $("#product-attributes-list").attr("scrollHeight") -
                  $("#product-attributes-list").height();

  $("#product-attributes-list").attr({scrollTop: (100- ui.value) * (maxScroll / 100)});
}

function closeMyWindow()
{
	$('#mask').hide();
	$('.window').hide();
}
