// <?php !! This fools phpdocumentor into parsing this file
/**
 * @version		$Id: ocie.js 1363 2010-08-01 16:53:43Z mic $
 * @package		OCIE
 * @copyright	Copyright (C) 2010 OSWorX. All rights reserved.
 * @author		mic - http://OSWorX.net
 * @license		GNU/GPL
 */

/**
 * used at dropdowns e.g. currrency, languages
 */
$(document).ready(function() {
	$('.switcher').click(function(){
		$(this).find('.option').slideToggle('fast');
	}),
	$('.switcher').mouseleave(function(){
		$(this).find('.option').slideUp('fast');
	})
});

/**
 * used for fancybox
 */
$(document).ready(function() {
	$("a.fancybox").attr('rel','group').fancybox();
	$("a.thickbox").fancybox({
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'speedIn'		:	600,
		'speedOut'		:	200,
		'overlayShow'	:	true,
		'easingIn'		: 'easeOutBack',
		'easingOut'		: 'easeInBack',
		'hideOnContentClick': false,
		'titlePosition'	: 'inside'
	});
});

/**
 * used for bookmarking
 */
function bookmark(url, title) {
	if (window.sidebar) { // firefox
    	window.sidebar.addPanel(title, url, "");
	} else if(window.opera && window.print) { // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} else if(document.all) {// ie
   		window.external.AddFavorite(url, title);
	}
};

/**
 * used to get/define URLs
 */
function getURLVar(urlVarName) {
	var urlHalves = String(document.location).toLowerCase().split('?');
	var urlVarValue = '';

	if (urlHalves[1]) {
		var urlVars = urlHalves[1].split('&');

		for (var i = 0; i <= (urlVars.length); i++) {
			if (urlVars[i]) {
				var urlVarPair = urlVars[i].split('=');

				if (urlVarPair[0] && urlVarPair[0] == urlVarName.toLowerCase()) {
					urlVarValue = urlVarPair[1];
				}
			}
		}
	}

	return urlVarValue;
};

/**
 * used define which tab is selected
 */
$(document).ready(function() {
	route = getURLVar('route');

	if (!route) {
		$('#tab_home').addClass('selected');
	} else {
		part = route.split('/');

		if (route == 'common/home') {
			$('#tab_home').addClass('selected');
		} else if (route == 'account/login') {
			$('#tab_login').addClass('selected');
		} else if (part[0] == 'account') {
			$('#tab_account').addClass('selected');
		} else if (route == 'checkout/cart') {
			$('#tab_cart').addClass('selected');
		} else if (part[0] == 'checkout') {
			$('#tab_checkout').addClass('selected');
		} else {
			$('#tab_home').addClass('selected');
		}
	}
});

/**
 * triggers key hit at search option
 * @see moduleSearch()
 */
$('#search input').keydown(function(e) {
	if (e.keyCode == 13) {
		moduleSearch();
	}
});

/**
 * starts search if key is hit
 * mic: fixed if search is empty or lenght <= 2
 * @see keydown
 */
function moduleSearch() {
	var url = 'index.php?route=product/search';
	var filter_keyword = $('#filter_keyword').attr('value')

	// fix mic - need to be fixed via external var - no php here!
	//if( filter_keyword == '<?php echo $text_keyword; ?>' ) {
	//	return;
	//}

	// mic: no submit if keyword is under 3 chars
	if( filter_keyword.length <= 2 ) {
		return;
	}

	if (filter_keyword ) {
		url += '&keyword=' + encodeURIComponent(filter_keyword);
	}

	var filter_category_id = $('#filter_category_id').attr('value');

	if (filter_category_id) {
		url += '&category_id=' + filter_category_id;
	}

	location = url;
};
