var jQ = jQuery.noConflict();

jQ(document).ready(function(){
    // Spusteni dropdown menu
    jQ("ul.sf-menu").superfish();
    
    imagePreview();

	// oznaceni externich odkazu v odkazovniku
	jQ('#detailLinker ul li a').filter(function() {
		return this.hostname && this.hostname !== location.hostname;
	}).after(' <img src="../img/external.gif"/>');

    // rozbalovani/zabalovani kategorii
	jQ("li.clickable ul").hide();
	
	jQ("li.clickable span").toggle(
		function() {
			jQ(this).siblings().show(400);
		},
		function() {
			jQ(this).siblings().hide(400);
		}
	);

	jQ("li:has(a.selected)").removeClass("inactive");

	// zakulaceni rohu boxu s podobnymi produkty
    jQ(".detailSimilarItem, .categoryProductItem").corner({
       radius: 6,
       antiAlias: true
    });

	//rozbalovani menu informacni sekce
	jQ("#information ul li ul").hide();
	jQ("#information ul li span").toggle(
		function() {
			jQ(this).next().show(400);
		},
		function() {
			jQ(this).next().hide(400);
		}
	);
    
    jQ("#openCloseDiscussion").click(function() {
       jQ("#detailDiscussion").toggle(400);
    });
    
    jQ(function(){
        jQ('a.newWindow').click(function(){
            window.open(this.href, 'console', 'width=600,height=450,location=no');
            return false;
        });
    });
    
    // schovani formu s dorucovaci adresou
    jQ("#delivery_address").hide();

    // otevreni dorucovaci adresy na registracnim formu
    jQ("#regForm_one_address").click(function() {
        jQ("#delivery_address").toggle(400);
    });
    
    // otevreni dorucovaci adresy na formu s udaji o uzivateli v kosiku
    jQ("#user_form_one_address").click(function() {
        jQ("#delivery_address").toggle(400);
    });

    jQ(function() {
        jQ('a[@rel*=jQlight-box]').lightBox();
    });

	
	function nl2br (str) {
		breakTag = '<br>';
		return (str + '').replace(/([^>]?)\n/g, '$1'+ breakTag +'\n');
	}


    //pretizeni alertu
    if(document.getElementById) {
      window.alert = function(txt) {
        jQ.prompt(nl2br(txt, true));
      }
    }
	
	//otevreni diskuze pokud adresa obsahuj #diskuze
	if(document.location.toString().match('#diskuze')) {
		jQ("#detailDiscussion").toggle(400);
	}
	
	//spusteni videa v ligtboxu
	if(document.location.toString().match('#play')) {
		myLightbox = new Lightbox();
		myLightbox.start(document.getElementById("videoPlay"));
	}

	//galerie u detailu zbozi
	jQuery('#detailGallery').jcarousel();

	//scrollovani produktu na hp
	function mycarousel_initCallback(carousel) {
		jQuery('.randomProductControl a').bind('click', function() {
		carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
		return false;
		});

		// Disable autoscrolling if the user clicks the prev or next button.
		jQuery('.randomProductControl a').bind('click', function() {
		carousel.startAuto(0);
		});

		jQuery('.randomProductControl a').bind('click', function() {
		carousel.startAuto(0);
		});

		// Pause autoscrolling if the user moves with the cursor over the clip.
		carousel.clip.hover(function() {
		carousel.stopAuto();
		}, function() {
		carousel.startAuto();
		});
	}

	jQ("#randomProduct").jcarousel({
		scroll: 1,
		visible: 1,
		auto: 5,
		wrap: 'last',
		initCallback: mycarousel_initCallback,
		buttonNextHTML: null,
		buttonPrevHTML: null
	});
	

	//Nacitani mest podle vybraneho regionu na strance s prodejci
	jQ("#chooseRegion").change(function() {
		var regionId = jQ("#chooseRegion option:selected").val();
		jQ("#cities").load("/map_city_loader.php?region=" + regionId);
	});

}); 