function checkWarranties() {
	var max = $('#warrantiesCount').val();
	for(var i = 0; i<max; i++) {
		var warranty = document.getElementById('warrantyItem_'+i);
		if(warranty.checked == true)	{
			return warranty.value;
		}
	}
	return "";
}

function setRecomendedValue(vote) {
	if(vote == 'yes') {
		$('#recomended').val('TRUE');
		$("#recomendedNo").css("text-decoration","none");
		$("#recomendedYes").css("text-decoration","underline");
	}
	else {
		$('#recomended').val('FALSE');
		$("#recomendedYes").css("text-decoration","none");
		$("#recomendedNo").css("text-decoration","underline");
	}
}
function triggerNotes(id, note, grade) {
	for( i = 1; i <= 5; i++ ) {
		$("#"+note+"_"+i).attr("src","/images/www/vote_star_big_off.gif");
		if ( i <=id){
			$("#"+note+"_"+i).attr("src","/images/www/vote_star_big_on.gif");
		}	
	}
	$("#" + grade).val(id);
}
function checkItemR(item) {
	$('#warrantiesSelected').val(item);
}
function setZoom(c){
	if($('#multimedia .tabs .zoom').hasClass('working')){}
	else if($('#multimedia .tabs .zoom').hasClass('notready')){
		$('#multimedia .tabs .zoom').addClass('working').removeClass('notready');
		startZoom();
	}
	else $('#multimedia .tabs .zoom .small:eq('+c+')').click();
}
function setFoto(c){
	$('#multimedia .tabs .foto .small:eq('+c+')').click();
}
function setVideo(c){
	$('#multimedia .tabs .video .small:eq('+c+')').click();
}
function set360(c){
	if($('#swf360').html()==''){
		var flashVar=$('#swf360').attr('rel');
		$('#swf360').flash({
			swf:'/images/www/swf/s360.swf',
			width:'100%',
			height:'100%',
			flashvars: {   xmlurl: flashVar   }
		});
	}
}
function setTab(id){
	if(!$('#menu-multimedia li.current').hasClass(id)){
		$('#menu-multimedia li.current').removeClass('current');
		$('#menu-multimedia a.'+id).parent().addClass('current');
		
		$('#multimedia .tabs .activ').removeClass('activ');
		$('#multimedia .tabs .'+id).addClass('activ');
		var t=parseInt($('#menu-multimedia a.'+id).attr('rel'));
		switch (id) {
		case 'foto':
			setFoto(t);
			break;
		case 'zoom':
			setZoom(t);
			break;
		case 'video':
			setVideo(t);
			break;
		case 's360':
			set360(t);
			break;
		default:
			break;
		}
	}
}

var sw=71;
var sh=63;
var cw=600;
var ch=532;

function showFlowPlayer(obj){
	$('#videoSwf').html('').append('<a style="display:block;width:'+obj.width+'px;height:'+obj.height+'px" href="'+obj.flv+'" id="flowPlayer"/>');
	flowplayer("flowPlayer", {src:"/images/www/swf/flowplayer-3.2.1.swf", wmode:'opaque'}, {
		clip: {
			autoPlay: false
		}	
	});
}
function showYouTube(obj){
	obj.swf=obj.swf.replace(/\&/g,'&amp;');
	$('#multimedia .tabs .video .big').css({
		top:(500-obj.height)/2,
		left:(620-obj.width)/2
	});
	$('#videoSwf').html('<object width="'+obj.width+'" height="'+obj.height+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param value="'+obj.swf+'" name="movie"><param value="transparent" name="wmode"><param value="true" name="allowfullscreen"><embed width="'+obj.width+'" height="'+obj.height+'" allowfullscreen="true" wmode="transparent" type="application/x-shockwave-flash" src="'+obj.swf+'"></object>');
}
function startZoomDrag(i){
	var small=$('#multimedia .tabs .zoom .small:eq('+i+')');
	var big=$('#multimedia .tabs .zoom .big div.img:eq('+i+')');
	var bigImg=big.children();
	
	var bw=bigImg.width();
	var bh=bigImg.height();
	
	if(bw/cw>bh/ch){
		var nw=parseInt(sw/(bw/cw));
		var nh=parseInt(sh/(bw/cw));
		big.css({
			width:bw,
			height:parseInt((bw/cw)*ch)
		});
		bigImg.css({
			'margin-top':parseInt(((big.height()-bh)/2))
		});
	}
	else {
		var nw=parseInt(sw/(bh/ch));
		var nh=parseInt(sh/(bh/ch));
		big.css({
			width:parseInt((bh/ch)*cw),
			height:bh
		});
	}
	big.attr({
		c:i
	});
	small.removeClass('notworking').children().not('.cur').remove();
	big.draggable({
		drag: function(event, ui) {
			var cur=$('#multimedia .tabs .zoom .active .cur');
			var top=-(ui.position.top/big.height())*sh;
			var left=-(ui.position.left/big.height())*sw;
			if(top>(sh-cur.height()))top=sh-cur.height();
			if(left>(sw-cur.width()))left=sw-cur.width();
			if(top<0)top=0;
			if(left<0)left=0;
			cur.css({
				top:top,
				left:left
			});
	  	}
	});
	
	small.click(function(){
		var c=$(this).attr('c');
		if($('#menu-multimedia .zoom').attr('rel')!=c || c=='0'){
			$('#multimedia .tabs .zoom .big div.img').hide();
			$('#multimedia .tabs .zoom .big img:eq('+c+')').show();
			$('#menu-multimedia .zoom').attr('rel',c);
			$('#multimedia .tabs .zoom .active').removeClass('active');
			$(this).addClass('active');
			var u=$(this).children('.cur');
			var bb=$('#multimedia .tabs .zoom .big .img:eq('+c+')');
			bb.css({
				top:-(parseInt(u.css('top'))/sh)*bb.height(),
				left:-(parseInt(u.css('left'))/sw)*bb.width()
			}).fadeIn();			
		}
	});
	small.children('.cur').css({
		opacity:0.5,
		width:nw,
		height:nh,
		left:parseInt((sw-nw)/2),
		top:parseInt((sh-nh)/2)
	}).draggable({
		containment: 'parent',
		drag: function(event, ui) {
			var bb=$('#multimedia .tabs .zoom .big .img');
			bb.css({
				top:-(ui.position.top/sh)*bb.height(),
				left:-(ui.position.left/sw)*bb.width()
			})
		}
	});
	if(i==0){
		$('#multimedia .tabs .zoom .small:eq(0)').click()	
	}
	startZoom();
	
}
function startZoom(){
	if($('#multimedia .tabs .zoom .notworking').length){
		var small=$('#multimedia .tabs .zoom .notworking:first');
		small.show();
		var img = new Image();
		$(img).load(function(){   
			$(this).hide();
			$('#multimedia .tabs .zoom .big').append('<div class="img"/>');
			$('#multimedia .tabs .zoom .big .img:last').append(this);
			startZoomDrag(small.attr('c'));
		}).error(function(){}).attr('src', small.attr('rel'));
	}else {
		$('#multimedia .tabs .zoom .rr').css({
			'background-image':'none'
		})
	}
}

function showMultimedia(rel){
	var v=rel.split('|');
	var sM='false';
	if($.cookie('sM')=='true')sM='true';
	if($('#productId').length)var href='/product-multimedia.ltr?sM='+sM+'&productId='+$('#productId').val();
	else href='/product-multimedia.ltr?sM='+sM+'&suiteId='+$('#suiteId').val();
	$.fancybox({
		href:href
	},{
		autoDimensions:	true,
		padding:0,
		centerOnScroll: false,
		onComplete:function(){
			$('#menu-multimedia li').hover(function(){
				if(!$(this).hasClass('current'))$(this).addClass('hovers');
			},function(){
				$(this).removeClass('hovers');
			});
			$('#menu-multimedia a').click(function(){
				setTab($(this).attr('class'));
			});
			if($('#multimedia .tabs .zoom').length){
				//startZoom(v);
			};
			if($('#multimedia .tabs .foto').length){
				$('#multimedia .tabs .foto .small').click(function(){
					$('#menu-multimedia .foto').attr('rel',$(this).attr('c'));
					$('#multimedia .tabs .foto .active').removeClass('active');
					$(this).addClass('active');
					if(!$('#multimedia .tabs .foto .big img').length){
						$('#multimedia .tabs .foto .big').append('<img alt=""/>');
					}
					if($('#multimedia .tabs .foto .big img').attr('src')!=$(this).attr('rel')){
						$('#multimedia .tabs .foto .big img').attr({
							src:$(this).attr('rel')
						})
					}
				});
			}
			if($('#multimedia .tabs .video').length){
				$('#multimedia .tabs .video .small').click(function(){
					$('#menu-multimedia .video').attr('rel',$(this).attr('c'));
					$('#multimedia .tabs .video .active').removeClass('active');
					$(this).addClass('active');
				});
			}
			if($('#multimedia .tabs .s360').length){
				//
			}
			$('#menu-multimedia a').attr('rel',0);
			$('#menu-multimedia a.'+v[0]).attr('rel',v[1]);
			setTab(v[0]);
		}
	});
}
function addMultimedia(){
	$('#add-photos .video, #add-photos .s360').removeClass('hide');
	var c=parseInt($('#add-photos').attr('c'));
	if(c>4){
		$('#add-photos').attr('c',$('#add-photos a').length);
	}else{
		addPhotosCarousel();
	}
	$.cookie('sM','true',{ path: '/', expires: 30 });
}

function addPhotosCarousel(){
	var t=$('#add-photos a').not('.hide').length;
	if(t>4){
		$('#add-photos').attr('c',t).attr('i',0);
		$('div.pC2, div.pC1').addClass('hide');
		$('a.pC1').show().removeClass('hide').click(function(){
			var i=parseInt($('#add-photos').attr('i'));
			var c=parseInt($('#add-photos').attr('c'));
			if(i>0){
				i--;
				$('#add-photos div').animate({left: i*(-55)},300,function(){
					$('#add-photos').attr('i',i);
					if(i>=0){
						$('a.pC2').addClass('pC2a');
					}
					if(i<(c-4)){
						$('a.pC1').removeClass('pC1a');
					}
				});
				
			}
		});
		$('a.pC2').show().removeClass('hide').addClass('pC2a').click(function(){
			var i=parseInt($('#add-photos').attr('i'));
			var c=parseInt($('#add-photos').attr('c'));
			if(i<(c-4)){
				i++;
				$('#add-photos div').animate({left: i*(-55)},300,function(){
					$('#add-photos').attr('i',i);
					if(i>0){
						$('a.pC1').addClass('pC1a');
					}
					if(i>=(c-4)){
						$('a.pC2').removeClass('pC2a');
					}
				});
			}
		});
	}else{
		$('a.pC1, a.pC2').addClass('hide');
	}
}
function mainSuiteBox(){
	var sS=$('.sharebookSuite');
	if(sS.text().length>0){
		var p=$('.photo-left').height()-15-$('.shareT').height();
		if($.browser.version=='6.0' || $.browser.version=='7.0')p+=10;
		sS.show().css('margin-top',p-sS.height());
	}
}
function startProduct(){
	if(isPrint=='false'){
		if($.cookie('sM')=='true')addMultimedia();
		addPhotosCarousel();
		$('.suite-product-name').tooltip({
			delay: 500,
			 track: true,
			 showURL: false
		});
		if($('.accessorieCard').length){
			$('.accessory-box').each(function(){
				var url=$(this).find('.cardLayerUrl').val();
				$(this).find('.accFancy').attr({
					href:url
				}).removeAttr('title');
			});
			accessoriesFancybox('.accFancy');
		}

		if($('#LayerView').length==0){
			setOS('.setOS');
		}
		depositories('a.depositories');
		$('#hiddenFancyLink').append('<a id="depositoriesBig" href="#"></a>');
		$("#depositoriesBig").fancybox({
			autoDimensions:	true
		});
		fancyDict("a.fancyDict");
		clipboardLink();
		$('#add-photos a').hover(function(){
			if(!$(this).hasClass('photoactive') && !$(this).hasClass('video') && !$(this).hasClass('s360')){
				$('#add-photos a').removeClass("photoactive");
				$(this).addClass("photoactive");
				var rel=$(this).attr('rel');
				var tmp=rel.split('|');
				if(tmp[2]!='' && tmp[2]!=undefined)var src=tmp[2];
				else var src=$(this).attr('href');
				var title=$(this).attr('title');
				$('#medium-photo-href a img').remove();
				$('#medium-photo-href a').attr('rel',rel).attr('href',src).prepend('<img/>').children('img').attr('alt', title).attr('src',src);
			}
		},function(){}).not('.photoswitch-suite').click(function(){
			showMultimedia($(this).attr('rel'));
			return false;
		});
		$('#medium-photo-href .productImage').click(function(){
			showMultimedia($(this).attr('rel'));
			return false;
		});
		accessoriecClick();
		$('a.fancyWarranty').fancybox({
			autoDimensions:	true,
			onComplete: function(){
				$.fancybox.resize()
			}
		});
	}
	accessoriesWrap('#accessoriesOnProduct');
	accessoriesWrap('#moreAccessoriesOnProduct');
	mainProdBox();
	mainSuiteBox();
	if(document.location.hash=='#opis'){
		$('#basic_tech_details').hide();
		$('#additional_tech_details').show();
	}
}

$(document).ready(function () {
	startProduct();
});