/*
 * Author: Martin Kappel
 * Company: Bioport.cz
 */
$(document).ready(function(){
	$(".external").attr("target","_blank");

	// menu
	$('#mainMenu > ul > li').hover(function(e){
		$(this).addClass('hovered')
	},function(e){
		$(this).removeClass('hovered')
	})

	// list nahledy produktu
	$('#cWrap .prodItem .prodThumbMore.sys-has_images').hover(function(e){
        inPrevLine = this;
        prevTmout = setTimeout('showPreview(inPrevLine);',200);
    },function(){
        clearTimeout(prevTmout);
    });
	$('#previewHolder').mouseleave(function(e){$(this).hide()});

	// nahledy vsech advanced atributu - EDITED BY COEX
	$('.advAttr').hover(function(e){
        if(jQuery(this).hasClass("sys-adv_attrs-show_more")) {
            attrLine = this;
            tmoutAttr = setTimeout('showAttr(attrLine);',300);
        }
    },function(e){
        if(jQuery(this).hasClass("sys-adv_attrs-show_more")) {
            clearTimeout(tmoutAttr);
        }
    });

	// search filters
	$('#sOptions').click(function(e){showSearchFilters(this);return false});
	$('#sOptionsButt').click(function(e){closeSearchFilters();return false});
	$('#overLay').click(function(){closeSearchFilters();return false});

	// product detail tabs
	$('#pMediaTabs a').click(function(e){return handlePTabs(this)})
	
	// select fix for ie7/8
	if($.browser.msie && $.browser.version < 9) {
		$('#id_parent_product_category, #id_product_category').mousedown(function(){
			$(this).parent().addClass('ieSelect');
		})
		$('#id_parent_product_category, #id_product_category').blur(function(){
			$(this).parent().removeClass('ieSelect');
		})
	}

});

// product details tabs handlers
function handlePTabs(prvek) {
	var prvek = $(prvek)
	var target = '#'+prvek.attr('rel')
	$('#pMediaTabs li').removeClass('active')
	prvek.parent().addClass('active')
	$('.tab').hide()
	$(target).fadeIn('fast')
	return false
}

//search filters
function scrollSearch(){
	// fixovana pozice searche
	var vyskaS = $('.search').outerHeight()
	$('.search').data('scroll',true).wrap('<div style="height:'+vyskaS+'px" class="scrollSearchToggle" />')
		$(window).scroll(function(e){
			$el = $('.search');
			if ($(this).scrollTop() > 187 && $el.css('position') != 'fixed' && $el.data('scroll')) {
				$el.addClass('searchScroll');
			}
			else
				if ($(this).scrollTop() < 187) {
					$el.removeClass('searchScroll')
				}
		})
	$('.closeSearch').click(function(){closeSearchScroll();return false})
}
function closeSearchScroll(){
	$('.search').removeClass('searchScroll').data('scroll',false)
}
function showSearchFilters(butt){

	// umisteni, velikost
	var butt = $(butt)
	var pos = butt.offset()
	var posW = butt.width()
	var posC = $('#header').offset()

	var tar = $('#lbFilter')
	if(tar.width()<340) {
		var offsetX = -415
	} else if (tar.width()<670) {
		var offsetX = -250
	}  else if (tar.width()<1000) {
		var offsetX = 14
	}
	tar.css({
		'top' : pos.top + 20 ,
		'left' : (posC.left - offsetX)
	}).show()

	var closeButt = $('#sOptionsButt')
	closeButt.css({
		'top' : pos.top,
		'left' : pos.left,
		'width' : posW
	}).show()

	$('#overLay').show()
}

function closeSearchFilters(){
	 $('#sOptionsButt').hide()
	 $('#lbFilter').hide()
	 $('#overLay').hide()
}

// preview v listu
function showPreview(el) {
	var prvek = $(el);
	var position = prvek.position();
    var preview_holder = $('#previewHolder').html('');
    preview_holder.css({'height': 270});
	preview_holder.css({'top':(position.top-80)+'px','left':(position.left-10)+'px'}).show();
    preview_holder.load(prvek.attr("rel"), function() {
        preview_holder.css({'height': 'auto'});
    });
}
function showAttr(el) {
	$('.advAttrCont').hide();
	var prvek = $(el);
	var position = prvek.position();
	var cont = $('<div class="clear advAttrCont"></div>')
	var obsah = prvek.html();
	cont.html(obsah)
		.appendTo('body')
		.css({'top':(position.top-6)+'px','left':(position.left-14)+'px'})
		.show()
		.mouseleave(function(e){$(this).remove()});
}

/* show login form - product add - step 1 - fixed by COEX */
function showLoginForm(p) {
	$('.pDetailsCont .col2').show();
	$(p).blur();
	return false
}

/*
 * gallery plugin for Exapro by kaplick@bioport.cz
 */
jQuery.fn.pGal = function() {

	this.each(function(gi){
		var gal = $(this);
		gal.addClass('pGl-'+gi);
		var numNodes = 1;
		var rootPos = 10;
		var actual = 0;
		//var numItem = gal.find('img').length

		// positioning of thumbnails on load
		gal.find('.tabPage').each(function(index){
			var w = 270;
			$(this).css('left',index*w);
			$(this).attr('id','pGl-'+gi+'-'+index);
			numNodes = index+1;
		})
	 	createPager(numNodes,actual)

		// creates pager butts and functionality
		function createPager(numNodes,actual){
			var pager = $('<div class="paging clear" id="pGlPager-'+gi+'" />');
			if( numNodes>1 && actual < numNodes-1 ) {
				var link = $('<a href="#" class="next">next</a>');
				link.click(function(e){
					gotoTab(actual+1);
					pager.remove();
					actual += 1;
					return false;
				})
				pager.append(link);
			}
			if( actual != 0 ) {
				var link = $('<a href="#" class="prev">previous</a>');
				link.click(function(e){gotoTab(actual-1);pager.remove();return false;});
				pager.append(link);
			}
			var info = $('<span class="info">'+(actual+1)+'/'+numNodes+'</span>');
			pager.append(info);
			gal.append(pager);
		}

		function gotoTab(targetTab){
			var pos = -targetTab*270+10;
			gal.find('.sliderCont').animate({'left':pos+'px'});
			createPager(numNodes,targetTab);
		}
	})
}
/*
 *  kapselect Plugin
 */
    jQuery.fn.kapselect = function(options){
        
        options = $.extend({
            render : function(option){
                return $('<li>',{ html : option.text()});
            },
            className : ''
        },options);

        return this.each(function(){

            // The "this" points to the current select element:

            var select = $(this);

            var selectBoxContainer = $('<div>',{
                //width     : (select.innerWidth()),
                className   : 'niceSel',
                html        : '<div class="selectBox"><span></span></div>'
            });

            var dropDown = $('<ul>',{className:'dropDown'});
            var selectBox = selectBoxContainer.find('.selectBox span');
            var selectBoxCont = selectBoxContainer.find('.selectBox');
            
            selectBox.css({
                width: (select.innerWidth()-32)
            })
            
            // Looping though the options of the original select element

            if(options.className){
                dropDown.addClass(options.className);
            }

            select.find('option').each(function(i){
                var option = $(this);

                if(i==select.attr('selectedIndex')){
                    selectBox.html(option.text());
                }

                // As of jQuery 1.4.3 we can access HTML5
                // data attributes with the data() method.

                if(option.data('skip')){
                    return true;
                }

                // Creating a dropdown item according to the
                // data-icon and data-html-text HTML5 attributes:

                var li = options.render(option);

                li.click(function(){

                    selectBox.html(option.text());
                    dropDown.trigger('hide');

                    // When a click occurs, we are also reflecting
                    // the change on the original select element:
                    select.val(option.val()).change();

                    return false;
                });

                dropDown.append(li);
            });

            selectBoxContainer.append(dropDown.hide());
            select.hide().after(selectBoxContainer);

            // Binding custom show and hide events on the dropDown:

            dropDown.bind('show',function(){

                if(dropDown.is(':animated')){
                    return false;
                }
                $('.niceSel .dropDown').hide();
                $('.niceSel .selectBox').removeClass('expanded');
                selectBoxCont.addClass('expanded');
                dropDown.slideDown('fast');

            }).bind('hide',function(){

                if(dropDown.is(':animated')){
                    return false;
                }

                selectBoxCont.removeClass('expanded');
                dropDown.hide();

            }).bind('toggle',function(){
                if(selectBoxCont.hasClass('expanded')){
                    dropDown.trigger('hide');
                }
                else {
                    dropDown.trigger('show');
                } 
            });
            
            selectBoxCont.click(function(){
                dropDown.trigger('toggle');
                return false;
            });

            // If we click anywhere on the page, while the
            // dropdown is shown, it is going to be hidden:

            $(document).click(function(){
                dropDown.trigger('hide');
            });

        });
    }

/*
 * ------------------------------------------------------------
 * ---------------------- obecne funkce -----------------------
 * ------------------------------------------------------------
 *
 * General
 * skript na kontrolu platne emailove adresy
 */
	function validateEmail(src) {
	    var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
	    return pattern.test(src);
	}
/*
 * General
 * logovani na firebug konzoli
 */
	var console;
	function log(hlaska) { if(console) { console.log(hlaska); } }
	function warn(hlaska) { if(console) { console.warn(hlaska); } }
	function info(hlaska) { if(console) { console.info(hlaska); } }
	function error(hlaska) { if(console) { console.error(hlaska); } }


