/*
Fitness and Fairies Core JavaScript
By: Junaid Bhura
www.junaidbhura.com
*/

// No conflict
$j = jQuery.noConflict();

/* DROP DOWN MENU FUNCTIONS START */
var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open() {
	jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = $j(this).find('ul').css('visibility', 'visible');
}

function jsddm_close() {
	if(ddmenuitem)
		ddmenuitem.css('visibility', 'hidden');
}

function jsddm_timer() { closetimer = window.setTimeout(jsddm_close, timeout); }

function jsddm_canceltimer() {
	if(closetimer) {
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}
/* DROP DOWN MENU FUNCTIONS END */

$j(document).ready(function(){
	// If less than ie6 return
	if(jQuery.browser.msie) {
		userAgent = $j.browser.version;
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		version = userAgent;
		
		if(version < 7) {
			sevenUp.plugin.black.test({ showToAllBrowsers: false }, false);
			return;
		}
	}
	
	/* DROP DOWN MENU START */
	$j('#nav_654457 > li').bind('mouseover', jsddm_open);
	$j('#nav_654457 > li').bind('mouseout',  jsddm_timer);
	/* DROP DOWN MENU END */
	
	// Make # in dropdown and footer menus do nothing
	$j('#nav_654457 a, #footer-navs ul li.title a').click(function(){
		if($j(this).attr('href') == '#')
			return false;
	});
	
	//  Initialize Backgound Stretcher
	var background = $j('body').css('background-image').replace('url("', '').replace('")', '').replace('url(', '').replace(')', '');
	$j(document).bgStretcher({
		images: [background], imageWidth: 900, imageHeight: 637,
		resizeProportionally: false
	});
	
	// Change currency symbol to INR
	$j('.productItem span, .cartSummaryItem, #the-info .saleprice, .browse-price li, .cart .price').each(function(){
		$j(this).html($j(this).html().replace(/Rs/g, '<span class="WebRupee">Rs.</span> '));
	});
	
	// Remove Skype plugin (formatting issues near "customer service" next to "online chat")
	window.setTimeout(function() {
		$j('.skype_pnh_container').html('');
		$j('.skype_pnh_print_container').removeClass('skype_pnh_print_container');
	}, 800);
	
	// Product page accordion
	if($j('#product-accordion').length != 0) {
		$j('#product-accordion .title').click(function(){
			if(!$j(this).is('.active')) {
				$j('#product-accordion .content').slideUp();
				$j('#product-accordion .title').removeClass('active');
				
				$j(this).parent().find('.content').slideDown();
				$j(this).addClass('active');
			}
		});
	}
	
	// Search box
	if($j('#CAT_ProductSearch').attr('value') == '')
		$j('#CAT_ProductSearch').attr('value', 'search');
	$j('#CAT_ProductSearch').focus(function(){
		if($j('#CAT_ProductSearch').attr('value') == 'search')
			$j('#CAT_ProductSearch').attr('value', '');
	});
	$j('#CAT_ProductSearch').blur(function(){
		if($j('#CAT_ProductSearch').attr('value') == '')
			$j('#CAT_ProductSearch').attr('value', 'search');
	});
	
	// Home Page Slider
	if($j('#home-slider').length != 0) {
		$j('#home-slider').nivoSlider({
			effect:'fade',
			animSpeed:500,
			pauseTime:4000
		});
	}
	
	// Home page newsletter fields
	if($j('#home-grid').length != 0) {
		$j('#home-grid #FullName').attr('value', 'Your full name here');
		$j('#home-grid #EmailAddress').attr('value', 'Your email here');
		
		$j('#home-grid #FullName').focus(function(){
			if($j(this).attr('value') == 'Your full name here')
				$j(this).attr('value', '')
		});
		$j('#home-grid #FullName').blur(function(){
			if($j(this).attr('value') == '')
				$j(this).attr('value', 'Your full name here')
		});
		
		$j('#home-grid #EmailAddress').focus(function(){
			if($j(this).attr('value') == 'Your email here')
				$j(this).attr('value', '')
		});
		$j('#home-grid #EmailAddress').blur(function(){
			if($j(this).attr('value') == '')
				$j(this).attr('value', 'Your email here')
		});
	}
});

// Function to fix breadcrumb and highlight active breadcrumb
function fixBreadcrumb() {
	$j('#breadcrumb span').html($j('#breadcrumb span').html().replace(/:/g, '>'));
	$j('#breadcrumb span a').first().css('cursor', 'default');
	$j('#breadcrumb span a').first().click(function(){
		return false;
	});
	$j('#breadcrumb a').last().addClass('active');
}

// Form validations
function checkHomeNewsletterForm() {
	if($j('#FullName').attr('value') == '' || $j('#FullName').attr('value') == 'Your full name here') {
		alert('Please enter your full name');
		$j('#FullName').focus();
		return false;
	}
	if($j('#EmailAddress').attr('value') == '' || $j('#EmailAddress').attr('value') == 'Your email here' || !validEmail($j('#EmailAddress').attr('value'))) {
		alert('Please enter a valid email address');
		$j('#EmailAddress').focus();
		return false;
	}
	
	return true;
}

function validEmail(str) {
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}
