$(document).ready(function(){

	$('form :checkbox[name=selectall]').click(function(){
		var chk = $(this).attr('checked');
		$('form :checkbox.selectable').attr('checked',chk);
	});
	
	$('#focus').focus();

	$('input[niceinput]').focus(function() {

		var obj = $(this).parent().parent().parent();
		obj.removeClass();
		obj.addClass('niceinput_edit');
	});

	$('input[niceinput]').blur(function() {

		var obj = $(this).parent().parent().parent();
		obj.removeClass();
		obj.addClass('niceinput');
	});
	
	$('select[niceinput]').focus(function() {

		var obj = $(this).parent().parent().parent();
		obj.removeClass();
		obj.addClass('niceinput_edit');
	});
	
	$('select[niceinput]').blur(function() {

		var obj = $(this).parent().parent().parent();
		obj.removeClass();
		obj.addClass('niceinput');
	});

	$('.nicetextarea_resizable textarea').focus(function() {

		var obj = $(this).parent().parent().parent().parent().parent().parent().parent().parent();
		obj.removeClass();
		obj.addClass('nicetextarea_resizable_edit');
	});

	$('.nicetextarea_resizable textarea').blur(function() {

		var obj = $(this).parent().parent().parent().parent().parent().parent().parent().parent();
		obj.removeClass();
		obj.addClass('nicetextarea_resizable');
	});
	
	$('textarea[nicetextarea]').focus(function() {

		var obj = $(this).parent().parent();
		obj.removeClass();
		obj.addClass('nicetextarea_edit');
	});
	
	$('textarea[nicetextarea]').blur(function() {

		var obj = $(this).parent().parent();
		obj.removeClass();
		obj.addClass('nicetextarea');
	});
	
	
	
	// wysylanie formulaze po nacisnieciu enter
	$().keypress(function (e){
		if(e.which==13){ 
			$(this).parents('form').submit();
		}
	});
	
	$('a[href]').click(function(){
		if(/del\//i.test($(this).attr('href'))) if(!confirm('Czy napewno usunać wybrany element?')) return false;
	});

	
	
	var p = -156;
	var free = 1;
	var interval;
	var constant = 156;
		
	$("#vp_prdshow_scroll").css('marginLeft', '-156px');
	automateMovement();
	
	function moveLeft() {
		free=0;
			p = p - constant;
			$("#vp_prdshow_scroll").animate({ 
				marginLeft:p+'px'
			}, 500 , 'linear', function() {
				if(p < -constant) {
					p = -constant;
					$("#vp_prdshow_scroll").css('marginLeft', p + 'px');
					$("#vp_prdshow_scroll img:first").insertAfter($("#vp_prdshow_scroll img:last"));
					free=1;
				}
			});
	}
	function moveRight() {
		free=0;
		p = p + constant;
			$("#vp_prdshow_scroll").animate({ 
				marginLeft:p+'px'
			}, 500 , 'linear', function() {
				if(p > -constant) {
					$("#vp_prdshow_scroll img:last").insertBefore($("#vp_prdshow_scroll img:first"));
					p = -constant;
					$("#vp_prdshow_scroll").css('marginLeft', p + 'px');
					free=1;
				}
				
			});
	}
	function automateMovement() {
		interval = setInterval(function(){		
			moveLeft();			
		},4000);
	}
	
	
	$("#left_bracket").click(function(){
		if(interval != null) {
			clearInterval(interval);
			interval = null;
			var t = setTimeout(function(){
				automateMovement();
				clearTimeout(t);
			}, 8000);
		}
		if(free == 1)
		moveRight();
    });
	$("#right_bracket").click(function(){
		if(interval != null) {
			clearInterval(interval);
			interval = null;
			var t = setTimeout(function(){
				automateMovement();
				clearTimeout(t);
			}, 8000);
		}
		if(free == 1)
		moveLeft();
    });
	
	$("#vp_prdshow_scroll img").hover(function() {
		$(this).animate({
			opacity: 0.5
		},100);
	}, function() {
		$(this).animate({
			opacity: 1.0
		},100);
	});
	
	function setExampleClr(btn) {
		btn.css('cursor', 'pointer');
		btn.css('background', '#03B1F0');
		btn.css('color', '#fff');
	}
	function setExampleDrk(btn) {
		btn.css('cursor', 'default');
		btn.css('background', '#fff');
		btn.css('color', '#333');
	}
	
	$('#vp_examples_menu span').hover(function() {
		setExampleClr($(this));
	}, function() {
		setExampleDrk($(this));
	});
	
	var sz_img_list = 0;
	function moveUp() {
	var height = document.getElementsByClassName("image")[0].offsetHeight;
	free=0;
		sz_img_list -= height;
			$("#sz_small_imgs_in").animate({ 
				marginTop: sz_img_list+'px'
			}, 300 , 'linear', function() {
				if(sz_img_list < 0) {
					sz_img_list = 0;
					$("#sz_small_imgs_in").css('marginTop', sz_img_list + 'px');
					$("#sz_small_imgs_in .image:first").insertAfter($("#sz_small_imgs_in .image:last"));
					
				}
				free=1;
			});
	}
	function moveDown() {
	var height = document.getElementsByClassName("image")[0].offsetHeight;
	free=0;
		sz_img_list += height;
		if(sz_img_list > -height) {
					$("#sz_small_imgs_in .image:last").insertBefore($("#sz_small_imgs_in .image:first"));	
					sz_img_list = -height;
					$("#sz_small_imgs_in").css('marginTop', sz_img_list + 'px');
				}
			sz_img_list += height;	
			$("#sz_small_imgs_in").animate({ 
				marginTop: sz_img_list+'px'
			}, 300 , 'linear', function() {
			;
				free=1;
			});
	}

	$('#sz_arr_up').click(function() {
		if(free == 1 && getElementsByClassName("image").length > 2)
		moveUp();
	});
	$('#sz_arr_down').click(function() {
		if(free == 1 && getElementsByClassName("image").length > 2)
		moveDown();
	});
	$("#sz_small_imgs_in .image").hover(function() {
		$(this).children('div:first-child').animate({
			opacity: 0.7
		},50);
	}, function() {
		$(this).children('div:first-child').animate({
			opacity: 1.0
		},50);
	});
	
	$("#sz_small_imgs_in img").click(function() {
		var x = $(this).attr('alt');

		$('#sz_bigimg').attr('href', x.toString());
		var x = $(this).attr('src');
		if(getElementsByClassName('subprdarr').length < 1 && document.getElementById('sz1') != undefined && getElementsByClassName('parts_con').length < 1) {
			$("#sz_small_imgs_in img").parent().css('background-image', 'url(http://www.vpartner.pl/template/img/szablon1/small_bg1.jpg)');
			var k = $(this).parent().css('background-image', 'url(http://www.vpartner.pl/template/img/szablon1/small_bg2.jpg)');
		}
		$('#sz_bigimg img').animate({
			opacity: 0.0
		}, 100, 'linear', function() {
			$('#sz_bigimg').empty();
			$('#sz_bigimg').append("<img src='"+ x +"' alt='' style='opacity:0.0;'/>");
			$('#sz_bigimg img').animate({
				opacity: 1.0
			}, 200);
		});
		
	});
	
	
	$('.vp_tooltip').animate({
			opacity : 0.0
		}, 10);
var toolTipOver_sz = false;
	function showTip_sz(obj, e) {
		var text = obj.next().html();
		
		$('.vp_tooltip_mid_content').html(text);
		
		
		var o = topLeftOffset(e.target);
		var tt = getElementsByClassName('vp_tooltip', 'table')[0];	
		var w = tt.offsetWidth > 350 ? 350 : tt.offsetWidth;
		var h = tt.offsetHeight > 150 ? 150 : tt.offsetHeight;
		//alert(w)
		x = o.x - tt.offsetWidth 
		y = o.y - tt.offsetHeight 
		
		$('.vp_tooltip').css('max-width', '350px');
		var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
		if(is_chrome) $('.vp_tooltip').css('width', '300px');
		$('.vp_tooltip').css('max-height', '150px');

		//alert(w)
		x = o.x - tt.offsetWidth 
		y = o.y - tt.offsetHeight 	
		$('.vp_tooltip').css('left', x);
		$('.vp_tooltip').css('top', y);
		
			$('.vp_tooltip').animate({
				opacity : 1.0
			}, 150, 'linear', function() {
				$(this).stop(true);
			});
		
	}
	function hideTip_sz(obj) {
		$('.vp_tooltip').animate({
			opacity : 0.0
		}, 150);

	}
	
	$('.multioption2').mouseover(function(e) {
		
		toolTipOver_sz = true;
		var handle = $(this).parent();
		var t = setTimeout(function(){
			if(toolTipOver_sz) {
				showTip_sz(handle, e);
			}
			clearTimeout(t);
		}, 100);
		
		
	});
	$('.multioption2').mouseout(function() {
		if(toolTipOver_sz) {
			hideTip_sz($(this));
		}
		toolTipOver_sz = false;
	});
	$('.multioption3').click(function(e) {
		
		toolTipOver_sz = true;
		var handle = $(this);
		var t = setTimeout(function(){
			if(toolTipOver_sz) {
				showTip_sz(handle, e);
			}
			clearTimeout(t);
		}, 100);
		
	});
	$('.multioption3').mouseover(function(e) {
		
		$(this).css('cursor', 'pointer');
		
	});
	$('#closeTip').mouseover(function(e) {
		
		$(this).css('cursor', 'pointer');
		
	});
	$('#closeTip').click(function() {
		if(toolTipOver_sz) {
			hideTip_sz($(this));
		}
		toolTipOver_sz = false;
	});
	
	$('div[class^="main_char_"]').click(function() {
		
	});
	
	$('div[id^="sz3partscon_"]').click(function() {
		$('div[id^="sz3partscon_"]').css('background', '#fff');
		$(this).css('background', '#f6f6f6');
		var handle = $('#subprod_'+$(this).attr('id').split("_")[1]);
		$('*[id^=subprod_]').animate({
			opacity : 0.0
		}, 300, 'linear', function() {
			$(this).hide();
			handle.css('opacity', '0.0');
			handle.show();
			handle.animate({
				opacity : 1.0
			}, 300);
		});
		
		if(document.getElementsByClassName('subprdarr').length > 0) {
			$('img.subprdarr').attr('src', '');
			$('img#subprdarr_' + $(this).attr('id').split("_")[1]).attr('src', 'http://www.vpartner.pl/template/img/szablon2/sarr_down.jpg');
			if($(this).attr('id').split("_")[1] == 0) $('#subprodu_left').css('background-image', 'url(http://www.vpartner.pl/template/img/szablon2/subprd_left_on.jpg)');
			else $('#subprodu_left').css('background-image', 'url(http://www.vpartner.pl/template/img/szablon2/subprd_left.jpg)');
		}
	
	});
	
	$('div[id^="szpartscon_"]').click(function() {
		$('div[id^="szpartscon_"]').removeClass('sz_parts_con');
		$('div[id^="szpartscon_"]').removeClass('sz_parts_conw');
		$('div[id^="szpartsr_"]').removeClass('sz_parts_r');
		$('div[id^="szpartsr_"]').removeClass('sz_parts_rw');
		$('div[id^="szpartsl_"]').removeClass('sz_parts_l');
		$('div[id^="szpartsl_"]').removeClass('sz_parts_lw');

		$('div[id^="szpartscon_"]').addClass('sz_parts_conw');
		$('div[id^="szpartsl_"]').addClass('sz_parts_lw');
		$('div[id^="szpartsr_"]').addClass('sz_parts_rw');
		
		
		
		$('div#szpartscon_'+$(this).attr('id').split("_")[1]).removeClass('sz_parts_conw');
		$('div#szpartsl_'+$(this).attr('id').split("_")[1]).removeClass('sz_parts_lw');
		$('div#szpartsr_'+$(this).attr('id').split("_")[1]).removeClass('sz_parts_rw');
		
		$('div#szpartscon_'+$(this).attr('id').split("_")[1]).addClass('sz_parts_con');
		$('div#szpartsl_'+$(this).attr('id').split("_")[1]).addClass('sz_parts_l');
		$('div#szpartsr_'+$(this).attr('id').split("_")[1]).addClass('sz_parts_r');
			
		var handle = $('#subprod_'+$(this).attr('id').split("_")[1]);
		$('*[id^=subprod_]').animate({
			opacity : 0.0
		}, 300, 'linear', function() {
			$(this).hide();
			handle.css('opacity', '0.0');
			handle.show();
			handle.animate({
				opacity : 1.0
			}, 300);
		});
		
		if(document.getElementsByClassName('subprdarr').length > 0) {
			$('img.subprdarr').attr('src', '');
			$('img#subprdarr_' + $(this).attr('id').split("_")[1]).attr('src', 'http://www.vpartner.pl/template/img/szablon2/sarr_down.jpg');
			if($(this).attr('id').split("_")[1] == 0) $('#subprodu_left').css('background-image', 'url(http://www.vpartner.pl/template/img/szablon2/subprd_left_on.jpg)');
			else $('#subprodu_left').css('background-image', 'url(http://www.vpartner.pl/template/img/szablon2/subprd_left.jpg)');
		}
	});
	
	$('div[id^="main_char_"]').click(function() {
		$('div[id^="main_char_"]').removeClass('main_char_a');
		$('div[id^="main_char_"]').removeClass('main_char_i');
		$('div[id^="main_char_"]').addClass('main_char_i');
		$('.main_cha_arr').remove();
		$('#main_cha_left').css('background-image', 'url(http://www.vpartner.pl/template/img/szablon2/main_char_leftd.jpg)');
		$('#main_cha_right').css('background-image', 'url(http://www.vpartner.pl/template/img/szablon2/main_char_rightd.jpg)');
		$(this).removeClass('main_char_i');
		$(this).addClass('main_char_a');
		$(this).prepend("<img class='main_cha_arr' src='http://www.vpartner.pl/template/img/szablon2/main_char_arr.jpg' alt='' />");
		
		if($(this).attr('id') == 'main_char_0') {
			$('#main_cha_left').css('background-image', 'url(http://www.vpartner.pl/template/img/szablon2/main_char_left.jpg)');
			$('#main_char_log').css('display', 'none');
			$('#main_char_opis').css('display', 'none');
			$('#main_char_nazwa').css('display', '');
			
		}
		if($(this).attr('id') == 'main_char_2') {
			$('#main_cha_right').css('background-image', 'url(http://www.vpartner.pl/template/img/szablon2/main_char_right.jpg)');
			$('#main_char_log').css('display', 'none');
			$('#main_char_nazwa').css('display', 'none');
			$('#main_char_opis').css('display', '');

		}
		if($(this).attr('id') == 'main_char_1') {	
			$('#main_char_opis').css('display', 'none');
			$('#main_char_nazwa').css('display', 'none');
			$('#main_char_log').css('display', '');
		}
		
	});
	
	
	
	active_menu();
	$('.zakladka2con .zakladka2').click(function() {
		var txt = $(this).children('div')[0];
		txt = txt.innerHTML;
		$('#prdShop_info').animate({
			opacity : 0.0
		}, 300, 'linear', function() {
			$('#prdShop_info').html(txt);
			$('#prdShop_info').animate({
				opacity : 1.0
			}, 300);
		});
	});
	
	$('.multioption4').next().children('img').click(function() {
		var handle = $(this).parent();
		var tip = handle.get(0);
		var of = tip.offsetHeight-6;
			handle.animate({
				height:'0px'
			}, 300, 'linear', function() {
				handle.css('height',of+'px');
				handle.css('display','none');
			});
		});
		var sz3_z = 1000;
	$('.multioption4').click(function() {
		var handle = $(this).next();
		$('.tooltip').each(function() {
			$(this).css('display', 'none')
		});
			
		var tip = $(this).next().get(0);
		tip.style.marginTop = $(this).parent().get(0).offsetHeight + 'px';
		tip.style.zIndex = sz3_z+1;
		tip.style.display = 'block';
		var of = tip.offsetHeight-6;
		tip.style.display = 'none';
		$(this).next().css('height', '0');
		
		
		
		
		$(this).next().animate({
			height:of
		}, 300);
		
		
	});
	
	$('#opis').hover(function() {
		var data = $(this).children()[1];
		data.style.display = 'block';
		data.style.marginTop = '8px';
		data.style.marginLeft = "-" + (data.offsetWidth - $(this).get(0).offsetWidth-3) + "px";
	}, function() {
		var data = $(this).children()[1];
		data.style.display = 'none';
	});
	$('#daneL').hover(function() {
		var data = $(this).children()[1];
		data.style.display = 'block';
		data.style.marginTop = '6px';
		data.style.marginLeft = "-" + (data.offsetWidth - $(this).get(0).offsetWidth) + "px";
	}, function() {
		var data = $(this).children()[1];
		data.style.display = 'none';
	});
	
	$('#sz4_zakladka_bar div[class^="sz4_zakladka"]').click(function() {
		if($(this).attr('class') == 'sz4_zakladka') {
			var handle = $(this);
			var id = $(this).attr('id');
			$('#sz4_content div[id^="sz4_info"]').animate({
				opacity : '0'
			}, 300, 'linear', function() {
				$('.sz4_zakladka_on').attr('class', 'sz4_zakladka');
				$(handle).attr('class', 'sz4_zakladka_on');
				$('#sz4_content div[id^="sz4_info"]').css('display', 'none');
				
				$('#sz4_info' + id).css('display', 'block');
				$('#sz4_info' + id).animate({
					opacity : '1'
				}, 300);
			});
			
		}
	});
	
	$('#sendbymail').click(function() {

		if($(this).attr('checked') == true) {
			$('.fakpocz').css('display', 'inherit')
		} else {
			$('.fakpocz').css('display', 'none')
		}
	});
	
	/*$("#page_pass").click(function() {
		$(this).attr('value', '');
	});
	$("#page_login").click(function() {
		$(this).attr('value', '');
	}); */
	
});

function getExample(num) {
	$.ajax({
		   type: "POST",
		   url: "http://www.vpartner.pl/visualize.html?example=" + num,
		   data: '',
		   success: function(msg){
			  $('#example_frame').html('');
			  $('#example_frame').html(msg);
		   }
		 });
}
function getProduct(num) {
	$.ajax({
		   type: "POST",
		   url: "http://www.vpartner.pl/visualize.html?product=_" + num,
		   data: '',
		   success: function(msg){
			  $('#example_frame').html('');
			  $('#example_frame').html(msg);
		   }
		 });
}

function active_menu() {
	var in_page = window.location.toString().slice(23);
	var ind = in_page.indexOf('?');
		var in_pagek = in_page;
		if(ind != -1)
			in_page = in_page.slice(0, ind);
	if(in_page == "productsBase.html" || in_page == "productsPrice.html" || in_page == "productsIntegration.html" || in_page == "productsExample.html" ) {
		$('#mv_bazap').css('background-position', '-22px 0');
	}
	else if(in_page == "printing.html" || in_page == "printingExample.html" || in_page == "printingPrice.html")
		$('#mv_cenowka').css('background-position', '-22px 450px');
	else if(in_page == "gams.html")
		$('#mv_gams').css('background-position', '-22px 300px');
	else if(in_page == "sms.html")
		$('#mv_sms').css('background-position', '-22px 150px');
	
}
window.onload = function() {
	getElementsByClassName('prdshow_img').each(function(elem) {
		var margw = '17px';
		var margh = '10px ';
		
		if(elem.offsetWidth > 0) {
		if(elem.offsetWidth < 150) {
			

			margw = (156 - elem.offsetWidth)/2 + 'px';
		}
		if(elem.offsetHeight < 110) {
			margh = (156 - elem.offsetHeight)/2 + 'px ';
		}

		} else {
			if(elem.offsetWidth < 150) {
				margw = (156 - elem.offsetWidth)/2 + 'px';
			}
			if(elem.offsetHeight < 110) {
				margh = (156 - elem.offsetHeight)/2 + 'px ';
			}
		}

		elem.style.margin = margh + margw;
		elem.onclick = function() {
			window.location = "http://www.vpartner.pl/productsExample.html?product=" + this.id;
		}
	});
	
	
	
}

function topLeftOffset(element) {
		var offset = {
			x : element.offsetLeft,
			y : element.offsetTop
		}
		var parent = element.offsetParent;
		while (parent) {
			offset.x += parent.offsetLeft;
			offset.y += parent.offsetTop;
			parent = parent.offsetParent;
		}
		return offset;
	}



function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = new Array;
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

Array.prototype.each = function(iterator) {
    for (var i = 0, length = this.length; i < length; i++)
      iterator(this[i]);
}
function calcHeight(f)
{

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

  
  alert(f.contentWindow.document.body.scrollHeight + px);

  
  
  
}

