﻿/* send to friend */
function sendToFriend(){
	var msg = $(".send-to-friend .message");
	if (msg.length > 0) {
		var inputs = $(".send-to-friend form").hide();
		msg.find(".sent-another").click(function(){
			inputs.show();
			msg.hide();
		});
	}
}

/* newsletter */
function newsletter(formSelector) {
	$(formSelector).submit(function(){
		SP.forms.submit(formSelector, function(html){
			Shadowbox.open({
				content: html,
				player: 'html',
				width: 550,
				height: 300
			});
		});
		return false;
	});
}

/* pridani do kosiku */
function addToBasket(form)
{
	var URL = '/pridat-do-kosiku.html';
	
	var $form = $(form);
	var product_id = $form.find('.add-product-id').val();
	var amount = $form.find('.add-amount').val();
	var variant_el = $form.find('.variant-combo input');
	
	var finalUrl = jQuery.query.set('add_product_id', product_id);
	
	finalUrl = finalUrl.set('add_product_count', amount);
	
	
	if (variant_el != null) {
		$(variant_el).each(function(idx, el) {
			finalUrl = finalUrl.set(el.name, el.value);	
		});
	}
	
	Shadowbox.open({
		content: "/pridat-do-kosiku.html" + finalUrl.toString(),
		player: 'iframe',
		width: 600,
		height: 350
	});
		
	/*function(html){
			$('#cart-state').html(html);
	});*/
	return false;
}


/* overeni podporovaneho prohlizece */
SP.utils.verifyBrowser();


/* rychle pridani produktu do kosiku - z vyhledavani */
function quickAddButton(id, el) {
	SP.forms.Button(id, {
		text: "Do košíku",
		click: function(){
			var btnEl = el.find(".x-button-text");
			var originalText = btnEl.text();
			btnEl.text("přidávám...");

			var form = el.parents(".quickadd");
			var product_id = form.find('.add-product-id').val();
			var product_amount = form.find('.add-amount').val();
			
			var url = "/rychle-pridani-do-kosiku.html?add_product_id="+product_id+"&add_product_count="+product_amount;
	
			$.get(url, function(html){
				var response = html.split("<!--split-->");
				btnEl.text(originalText);
				$("#header .basket-thumb").html(response[0]);
				$("#basket-detail").html(response[1]);
				$("#notifications-holder").html(response[2]);
				
				el.parents(".x-quick-search-results").hide();
				el.parents(".quickadd-wrapper").find("#basket-quick-add-input").val("").blur();
			});
		}
	});
}


/**
 * GIFTFINDER
 */
Giftfinder = (function(){
	var totalSteps = 4;
	var step = 1;
	var steps = [{
		previous: false,
		next: false
	},{
		previous: "Zpět",
		next: false
	},{
		previous: "Zpět",
		next: false
	},{
		previous: "Zpět",
		next: false
	}];
	var values = [];
	
	var titles, contents, prev, prevText, next, nextText;
	
	return {
		init: function(){
			titles = $('.giftfinder-steps .step');
			contents = $('.giftfinder-step-contents .step');
			prev = $("#previous-button");
			next = $("#next-button");
			prevText = prev;
			nextText = next;

			Giftfinder.step(1);
		},
		
		buttons: function(){
			// previous
			if (steps[step - 1].previous == false) {
				prev.hide();
			} else {
				prevText.text(steps[step - 1].previous);
				prev.show();
			}
			
			// next
			if (steps[step - 1].next == false) {
				next.hide();
			} else {
				nextText.text(steps[step - 1].next);
				next.show();
			}
		},
		
		titles: function(){
			for (var i = 0; i < totalSteps; i++) {
				var title = titles.eq(i);
				if (i < step - 1) {
					title.addClass('filled');
				} else {
					title.removeClass('filled');
				}
				
				if (i == step - 1) {
					title.addClass('active');
				} else {
					title.removeClass('active');
				}
				
				if (SP.isArray(values[i]) && !SP.isBlank(values[i][0])) {
					title.find(".value").text("("+values[i][0]+")").show();
				} else {
					title.find(".value").hide();
				}
			};
		},
		
		contents: function(){
			for (var i = 0; i < totalSteps; i++) {
				var content = contents.eq(i);
				if (i == step - 1) {
					content.show();
				} else {
					content.hide();
				}
			};
		},
		
		step: function(num){
			step = num;

			if (step == 3) {
				Giftfinder.swapImages();
			}

			if (step == 4) {
				Giftfinder.filter();
				Giftfinder.swapMainImage();
			}

			Giftfinder.titles();
			Giftfinder.contents();
			Giftfinder.buttons();
		},
		
		next: function(value){
			if (value){
				values[step - 1] = value;
			} else {
				delete values[step - 1];
			}
			
			if (step == totalSteps) {
				Giftfinder.finish();
				return
			}
			
			Giftfinder.step(step + 1);
		},
		
		previous: function(){
			if (step == 1) return;
			
			Giftfinder.step(step - 1);
		},
		
		finish: function(){
			var params = [];
			params.push("gender="+values[1][1]);
			params.push("forAgeFrom="+values[2][1]);
			params.push("forAgeTo="+values[2][2]);
			params.push("fulltext="); // reset fulltext

			contents.eq(3).find('input:checkbox:checked').each(function(){
				params.push("hobby_"+this.name+"=1");
			});
			
			location.href = "/hledani.html?"+params.join("&");
		},
		
		filter: function(){
			var young = ["Z1", "Z2", "Z3", "Z4", "Z5"];
			var boysOnly = ["Z15"];
			var girlsOnly = [];
			
			var age = parseFloat(values[2][2]);
			var isYoung = (age < 3 && age > 0);
			var isBoy = (values[1][1] == "boy");
			
			contents.eq(3).find('.hobby').each(function(){
				var wrapper = $(this);
				var input = wrapper.find('input');

				if ((isYoung && young.indexOf(input.attr("rel")) >= 0) ||
					(!isYoung && young.indexOf(input.attr("rel")) == -1)) {
					if ((!isBoy && boysOnly.indexOf(input.attr("rel")) >= 0) ||
						(isBoy && girlsOnly.indexOf(input.attr("rel")) >= 0)) {
						wrapper.hide();
					} else {
						wrapper.show();

						var boyLabel = wrapper.find("label[rel=boy]");
						var girlLabel = wrapper.find("label[rel=girl]");
						if (!isBoy && !SP.isBlank(girlLabel.text().trim())) {
							girlLabel.show();
							boyLabel.hide();
						} else {
							girlLabel.hide();
							boyLabel.show();
						}
					}
				} else {
					wrapper.hide();
				}
				
				input.attr('checked', false);
			});

			// prohod nadpis
			var boyHeading = contents.eq(3).find("h2[rel=boy]");
			var girlHeading = contents.eq(3).find("h2[rel=girl]");
			if (isBoy) {
				girlHeading.hide();
				boyHeading.show();
			} else {
				girlHeading.show();
				boyHeading.hide();
			}
		},
		
		swapImages: function(){
			var isBoy = (values[1][1] == "boy");
			var boyPhotos = contents.eq(2).find('a img[rel=boy]');
			var girlPhotos = contents.eq(2).find('a img[rel=girl]');

			if (isBoy) {
				girlPhotos.hide();
				boyPhotos.show();
			} else {
				girlPhotos.show();
				boyPhotos.hide();
			}
		},
		
		swapMainImage: function(){
			var isBoy = (values[1][1] == "boy");
			contents.eq(3).toggleClass("image-girl", !isBoy)
		},
		
		fillFromUrl: function(){
			// pouze pokud uz uzivatel neudelal nejaky krok
			// a v url jsou parametry, ze kterych brat informace
			if (step == 1 && typeof $.query.get('gender') == "string" && $.query.get('gender').length > 0) {
				Giftfinder.next(); // 1 -> 2
				
				// gender
				var g = $.query.get('gender');
				var gEl = contents.eq(1).find('a[rel='+g+']');
				if (gEl.size() == 1) {
					Giftfinder.next([gEl.find('span').text(), g]);
				
					// age
					var af = $.query.get('forAgeFrom');
					var at = $.query.get('forAgeTo');
					var aEl = contents.eq(2).find('a[rel='+af+','+at+']');
					if (aEl.size() == 1) {
						Giftfinder.next([aEl.find('span').text(), af, at]);
						
						// hobbies
						contents.eq(3).find('.hobby input').each(function(){
							var inp = $(this);
							var id = inp.attr('name');
							var checked = $.query.get('hobby_'+id) == "1";
							inp.attr('checked', checked);
						})
					}
				}
			}
		}
	}
})();


/**
 * Shadowbox
 */
function shadowboxInit(){
	/*
	 * Lightbox-like Shadowbox behavior
	 * @author  Miroslav Raska
	 * @version 20100918
	 * @require Shadowbox
	 */
	(function changeSkin() {
		var S = Shadowbox;
	
	    function toggleNav(id, on){
	    	var el = document.getElementById('sb-nav-' + id);
	    	if (el) el.style.display = on ? '' : 'none';
		}
	
		function updateNav(obj) {
			var len = S.gallery.length;
			var n,p;
			if (len > 1){
				if (S.options.continuous) {
					n = p = true; // show both
				} else {
					n = (len - 1) > S.current; // not last in gallery, show next
					p = S.current > 0; // not first in gallery, show previous
				}
			}

			toggleNav('content-previous', p);
			toggleNav('content-next', n);
		}
		
		S.options.onChange = updateNav;
		S.options.onOpen = updateNav;

		var prevHTML = '<a id="sb-nav-content-previous" href="#predchozi" onClick="this.blur();Shadowbox.previous();return false;"></a>';
		var nextHTML = '<a id="sb-nav-content-next" href="#dalsi" onClick="this.blur();Shadowbox.next();return false;"></a>';

		S.skin.markup = S.skin.markup.replace(/(<div[^>]+id="sb-body"[^>]*>)/, "$1"+prevHTML+nextHTML);

		var closeHTML = '<a id="sb-nav-content-close" href="#zavri" onClick="Shadowbox.close();return false;"></a>';
		S.skin.markup = S.skin.markup.replace(/(<div[^>]+id="sb-title"[^>]*>)/, "$1"+closeHTML);
	})();


	Shadowbox.init({
		players: ['html', 'iframe'],
		overlayOpacity: 0.32,
	    displayNav: false
	});
}


function createContactMap(id){
    var map = new google.maps.Map(document.getElementById(id), {
      zoom: 14,
      center: new google.maps.LatLng(49.780691,18.172245),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();
    infowindow.setContent("<strong>Rappa s.r.o.</strong><br/>"+
						  "Za Humny 1082/1<br/>"+
						  "725 25 Ostrava-Polanka");
    
    var marker = new google.maps.Marker({
      position: new google.maps.LatLng(49.780691,18.172245),
      map: map,
      title: "Rappa s.r.o."
    });

    google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map, marker);
    });
    infowindow.open(map, marker);
}

if (SP.isIE && SP.browserVersion == 8.0) {
	$(document).ready(function(){
		$(".product-action-image a").each(function(){
			var self = $(this);

			if (self.parents("#tab-new").size() == 0 && self.parents("#tab-action").size() == 0) {
				var img = self.find('img');
				self.width(img.width());
				self.height(img.height());
				self.css({
					position: 'relative',
					display: 'inline-block'
				});
				img.css('position', 'absolute');
				img.load(function(){
					self.width(img.width());
					self.height(img.height());
				});
			}
		});	
		$(".left-menu-flash a").each(function(){
			var self = $(this);

				var img = self.find('img');
				self.width(img.width());
				self.height(img.height());
				self.css({
					position: 'relative',
					display: 'inline-block'
				});
				img.css('position', 'absolute');
		});	
		$(".page-category .product-action-image a").each(function(){
			var self = $(this);

				var img = self.find('img');
				self.width(img.width());
				self.height(img.height());
				self.css({
					position: 'relative',
					display: 'inline-block'
				});
				img.css('position', 'absolute');
		});	
	});
}

