/*
 * General js stuff
 */


function $() {
        
	if (arguments.length>1) {
		return false;
	}
	if (arguments.length<1) {
		return false;
	}
    
	var element = arguments[0];
    var rez=element;
    if (typeof element == 'string') {
        if (document.getElementById) 
        {
            rez = document.getElementById('ctl00_cpBody_'+element);            
            if (rez == null) {rez = document.getElementById(element); }
        } else if (document.all) {
            rez = document.all['ctl00_cpBody_'+element];
            if (element == null) {rez = document.all[element]; }
        }
    }
	return rez;
}
function changeCulture(culture)
{
        var hasChanged = Layout.ChangeCulture(culture);

        if (hasChanged.value)
        {
            document.location.href = document.location;
        }
}
function Search()
{
    n = document.getElementById('ctl00_txSearch').value;
    document.location = "/rezultateleCautarii.aspx?n="+n;
}

function FilterSort()
{
    alert("FilterSort");
}

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}


function toggleElement(handle,content,additionalContent){
    if (additionalContent) { 
        if (handle.is(':checked')) {
            additionalContent.fadeOut('fast',
                function(){
                    content.fadeIn('fast');
                }
            );   
        } else {
            content.fadeOut('fast',
                function(){
                    additionalContent.fadeIn('fast');
                }
            ); 
        }
    
    } else {
        if (handle.is(':checked')) {
            content.fadeIn('fast');
        } else {
            content.fadeOut('fast');
        }
    }
}


function trimiteNewsletter() {
    (function($) {
        var email = $('#ctl00_txNewsletter').val();
        if (!isValidEmail(email)) {
            alert(getJSPH("Newsletter_Va rugam specificati o adresa de Email valida\n"));
        } else {
            document.location.href = '/newsletter.aspx?newEmail='+email;
        }
     })(jQuery)
}

 
//jQuery.noConflict();

jQuery(function($) { 
	
	
	/*
	 * Inline Shopping Cart
	 */
	
	// We hide it initially
	$('#inlineCart').hide();
	
	$('#buyButtons').hide();
	$('#inlineCart .total').hide();
	$('#inlineCart ul').hide();
	
	
	//Add event handler to open the cart on hover
	$('#inlineCartWrapper').hoverIntent(
		function(){
			$('#inlineCartWrapper').addClass('active');
			$('#inlineCart').show();
		},
		function(){
			$('#inlineCart').hide();
			$('#inlineCartWrapper').removeClass('active');
		}
	);
	
	//Only let users type in numeric characters
	$('#inlineCartWrapper').find('input').numeric();
	$('#productList input, #buy input').numeric();
	
	//Clone the Finalize and How To links into the small Cart
	//We do this to avoid duplicate links. No need of a js fallback => without js, noone is able to see the cart
	$('#inlineCartBar').find('a.finalizeOrder, a.howToBuy').clone().appendTo($('#buyButtons'));
	
	$('.buy input').numeric();
	
	
	
	/*
	 * AboutNav Menu
	 */
	var aboutNav = $('#aboutNav ul');
	var children = aboutNav.find('li>ul>li');
	
	//Setting children classes
	if (children.length >= 1) {
	    children.parent().parent().addClass('children');
	} 
	
	var active = aboutNav.find('li.active');
	active.parents('li').addClass('active');
	
	//Removing the empty ul tags
	aboutNav.find('li').not('.children').find('>ul').remove();
	

	/*
	 * IE fix
	 */
	//Filter IE
	if ($.browser.msie) {
	    $('#nav ul>li:last-child').addClass('last-child');
		$('#nav ul>li>ul>li:last-child').addClass('last-child');
		$('#nav ul>li>ul>li:first-child').addClass('first-child');
		$('#tabs>ul>li>ul>li:last-child').addClass('last-child');
		$('#tabs>ul>li>ul>li>ul>li:last-child').addClass('last-child');
		$('#aboutNav>ul>li>ul>li:last-child').addClass('last-child');
		
		
		$('input:text, input:password, textarea')
		    .focus(function(){ $(this).addClass('focus'); })
		    .blur(function(){ $(this).removeClass('focus'); });
		
		//Filter IE6
		if ($.browser.version == 6) {
			$('#tabs>ul>li')
				.mouseover(function(){$(this).addClass('hover');})
				.mouseout(function(){$(this).removeClass('hover');});
			$('#tabs>ul>li>ul>li')
				.mouseover(function(){$(this).addClass('hover');})
				.mouseout(function(){$(this).removeClass('hover');});
			
			$('#aboutNav>ul>li.children')
				.mouseover(function(){$(this).addClass('hover');})
				.mouseout(function(){$(this).removeClass('hover');});
		}
	}
	
	
	//AboutNav separators
	aboutNavLi = $('#aboutNav>ul>li').not(':first-child').not('.children');
	aboutNavLi.prepend(' | ');
	aboutNavLiChildren = $('#aboutNav>ul>li.children').before(' | ');
	
	
	/*
	 * Top Menu
	 */
	var tabs = $('#tabs ul');
	var children = tabs.find('li>ul>li');
	
	//Setting children classes
	if (children.length >= 1) {
	    children.parent().parent().addClass('children');
	} 
	
	//Removing the empty ul tags
	tabs.find('li').not('.children').find('>ul').remove();
	
	
	
	
	/*
	 * Navigation Animation
	 */
	var nav = $('#nav ul');
	var active = nav.find('li.active');
	
	//Setting active classes
	active.parents('li').addClass('active');
	
	//Hiding non active elements
	nav.find('li').not('.active').find('>ul').hide();
	
	//Hiding empty ul tags
	nav.find('ul:has(a)').addClass('full');
	nav.find('ul').not('.full').hide();
	
	//Setting a click event on level 1 & 2 links
	//which will return false as only lvl 3 links are real
	var a = nav.find('>li>a');
	a.click(function(){
		var li = $(this).parent();
		
		if (li.is('.active')) {
		    $('ul',li).slideUp('fast');
		    li.removeClass('active');
		} else {
		
		    //Hiding other branches
		    var siblings = li.siblings();
		    siblings.find('>ul').slideUp('fast');
    		
		    //Removing the 'active' class from the other branches
		    siblings.removeClass('active');
    		
//	   		li.find('>ul:has(a)').addClass('full');	
    		
		    //Showing this particular branch
		    if (li.find('>ul').is('.full')) {
		        li.find('>ul').slideDown('fast');
		    } else {
		        li.find('>ul').hide();
		    }
    		
		    //Setting the 'active' class
		    li.addClass('active');	    
		}
		
		//Returning false as this link has only animation purposes
		var children = li.find('a');
		if (children.length != 1) {
		    return false;    		
		}
	});
	
	var corporate = $('#nav>#corporate>ul>li');
	corporate.not('.active').hide();
	
//	$('#nav ul li.active ul:has(a)').addClass('full');
//	$('#nav>ul>li').find('ul').not('.full').hide();

    
    /*
	 * Breadcrumbs
	 */
    $('#nav').find('li.active>a').clone().appendTo($('#breadcrumbs'));
    $('#aboutNav').find('li.active>a').clone().appendTo($('#breadcrumbs'));

	
	
	/*
	 * Promotii Tabs
	 */
	//Creating the tabs
	var promotii = $('#promotii > ul');
	promotii.tabs();
	
	//Changing the tabs automatically every couple of seconds
    promotii.tabs('rotate',5000);
	
	
	/*
	 * Language selector
	 */
	$('#lang')
	    //Change language on click
	    .find('a').click(function(){
	        var ul = $(this).parent().parent();
	        
	        
	        
	        if (ul.is('.collapsed')){
	            //Overlay
	            //$('body').append('<div class="lang-overlay"></div>');
//	            $('body>.lang-overlay').click(function(){
//	                ul.addClass('collapsed');
//	                ul.find('li').not('.active').hide();
//	                $('body>.lang-overlay').removeClass('lang-overlay').remove();
//	            });
//				
//	            ul.find('li').show();
//				
//	            ul.removeClass('collapsed');
	        } else {
//	            $('body>.lang-overlay').removeClass('lang-overlay').remove();
	            var li = $(this).parent();
	            if (li.is('.active')) { 
//	                li.siblings('li').hide();
//	                ul.addClass('collapsed'); 
	            } else {
	                var lang = $(this).attr('href').substring(1);
	                //changeCulture(lang);
	                document.location = "/"+lang;
	            }
	        }
	        return false;
	    }).end()
	    
	    //Hides the non-active elements
//	    .find('li.active').siblings('li').hide()
//	    .parent('ul').addClass('collapsed')
	    
	//Empty on Focus
	$('#newsletter').emptyOnFocus();
	
	
	/*
	 * Search Box
	 */
	var searchLabel = $('#search label');
	var labelText = searchLabel.text();
	var searchInput = $('#search input:text');
	
//	//Setting the label to overlay the actual input
//	searchLabel.show();
//	
//	//Hiding the label when focusing the field
	searchInput
//		.focus(function(){
//			searchLabel.hide();
//		})
		
    //Submitting on enter
		.keyup(function(e){
		    if (e.keyCode == 13) {
		       $(this).siblings(':button').click();
		    }
		});
	
//	//Reshowing it if there is no input
//		.blur(function(){
//			if ($(this).val() == '') {
//				searchLabel.show();
//			}
//		});
	
	$('#search').emptyOnFocus();
});
