
jQuery(document).ready(function(){
	
	initTipsy();
	
	// MENU (temp)
	jQuery('.menu li').click(function() {
		//alert('menu');
		jQuery('.menu li').removeClass('current');
		jQuery(this).addClass('current');
	});
	
	
	// TEXT FIELDS 
	jQuery('.txtField').focus(function() {
		jQuery(this).addClass('focus');
	});
	
	jQuery('.txtField').focusout(function() {
		jQuery(this).removeClass('focus');
	});
	
	
	// ANIMATE SWITCH 
	jQuery('.switch').click(function() {
		btn = jQuery(this).find('.button');
		if(jQuery(btn).attr('rel') == 'on') {
			jQuery(btn).animate({
				left: '0'
			}, 100, function() {
				//alert('test');
				jQuery(btn).removeClass('on');
				jQuery(btn).addClass('off');
				jQuery(btn).attr('rel','off');
			});
		}
		else {
			jQuery(btn).animate({
				left: '29'
			}, 100, function() {
				//alert('test');
				jQuery(btn).removeClass('off');
				jQuery(btn).addClass('on');
				jQuery(btn).attr('rel','on');
			});
		}
	});
	
	
	// PLATFORMS
	jQuery('.platforms .icon').click(function() {
		jQuery(this).removeClass('hover');
		if(jQuery(this).attr('rel') == 'active') {
			jQuery(this).removeClass('active');
			jQuery(this).attr('rel','');
		}
		else {
			jQuery(this).addClass('active');
			jQuery(this).attr('rel','active');
		}		
	});
	
	jQuery('.platforms .icon').hover(function() {
		jQuery(this).addClass('hover');
	});
	
	jQuery('.platforms .icon').mouseleave(function() {
		jQuery(this).removeClass('hover');
	});
	
	
	// REVIEW SCREEN SHOTS
	jQuery(".review .details .screenshots a").colorbox({
		transition:'fade',
		rel:'screenGallery' 
	});
	
	
	
	/*
	jQuery(".image").colorbox({
		transition:"fade",
		rel:'gallery' 
	});
	*/
	
	// COLORBOX 
	jQuery('.cbInit').colorbox();
	
	jQuery('.head .toggle').click(function() {
		var box = jQuery(this).parent().parent().parent();
		if(jQuery(this).attr('rel') == 'max') {
			jQuery(box).find('.main').slideDown('fast');
			jQuery(this).removeClass('max');
			jQuery(this).addClass('min');
			jQuery(this).attr('rel','min');
		} 
		else {
			jQuery(box).find('.main').slideUp('fast');
			jQuery(this).removeClass('min');
			jQuery(this).addClass('max');
			jQuery(this).attr('rel','max');
		}
	});
	
	// categories page
	jQuery('.categoriesPage .entry').hover(function() {
		jQuery(this).find('.catIconL').addClass('hover');
	});
	
	jQuery('.categoriesPage .entry').mouseleave(function() {
		jQuery(this).find('.catIconL').removeClass('hover');
	});
	
	// PROMO 
	swictchPromo(0);
	jQuery('.promo .nav .icon').click(function() {
		id = jQuery(this).attr('rel');
		swictchPromo(id);
	});
	
	// feat categories
	jQuery('.featCat a').hover(function() {
		jQuery(this).find('.catIconM').addClass('hover');
	});
	
	jQuery('.featCat a').mouseleave(function() {
		jQuery(this).find('.catIconM').removeClass('hover');
	});
	
	// remove categories
	jQuery('.ctrlPanel .categories .catBtn').click(function() {
		jQuery(this).remove();
	});
	
	// ctrlPanel COUNTRY
	jQuery('.ctrlPanel .country .settings a').click(function() {
		jQuery('.ctrlPanel .country .details').slideDown();
	});
	
	jQuery('.ctrlPanel').mouseleave(function() {
		jQuery('.ctrlPanel .country .details').slideUp();
		jQuery('.ctrlPanel .country .map .label').fadeOut();
	});
	
	jQuery('.ctrlPanel .country .map').hover(function() {
		jQuery('.ctrlPanel .country .map .label').fadeIn();
	});
	
	
	/*
	jQuery('.ctrlPanel .country .details').hover(function() {
		//jQuery('.ctrlPanel .country .details').stop();
		jQuery('.ctrlPanel .country .details').slideDown();
	});
	*/
	
});

function reInitScripts() {
	initTipsy();
	initAddCategory();
}


function swictchPromo(id) {
	jQuery('.promo .entry').hide();
	jQuery('.promo .entry_'+id).show();
	jQuery('.promo .nav .icon').removeClass('current');
	jQuery('.promo .nav .icon_'+id).addClass('current');
}


function initTipsy() {
	// TIPSY
	jQuery('.tipsyS').tipsy({
		gravity: 's',
		html: 'true',
		fade: true
	});
	
	jQuery('.tipsyW').tipsy({
		gravity: 'w',
		html: 'true',
		fade: true,
		delayIn: 800
	});
	
	jQuery('.tipsyE').tipsy({
		gravity: 'e',
		html: 'true',
		fade: true,
		delayIn: 800
	});
}


function cbClose() {
	jQuery.colorbox.close();
	jQuery('.tipsy').remove();
}


// PRELOADINGBAR
function showPreload(txt) {
	if(txt) {
		jQuery('.loadingBar .default').hide();
		jQuery('.loadingBar .custom').show();
		jQuery('.loadingBar .custom').html(txt);
	} 
	jQuery('.loadingBar').animate({
		top: '0'
	}, 50, function() {
		// TEMP
		//hidePreload_TEMP();
	});	
}

function hidePreload() {
	jQuery('.loadingBar').animate({
		top: '-70px'
	}, 150, function() {
		jQuery('.loadingBar .default').show();
		jQuery('.loadingBar .custom').hide();
	});
}

function hidePreload_TEMP() {
	jQuery('.loadingBar').delay(2000).animate({
		top: '-70px'
	}, 250, function() {
		jQuery('.loadingBar .default').show();
		jQuery('.loadingBar .custom').hide();
	});
}


// POST SUBSCRIPTION
function postSubscription() {
	jQuery('.newsletter').find('.default').hide();
	jQuery('.newsletter').find('.feedback').fadeIn('fast');
}


// LIGHTBOX ADD CATEGORY
function initAddCategory() {
	/*
	jQuery('.addCategory').find('.entry').hover(function(){
		jQuery(this).find('.btnS').addClass('hover');
		jQuery(this).find('.catIconM').addClass('hover');
	});
	jQuery('.addCategory').find('.entry').mouseleave(function(){
		jQueryjQuery(this).find('.btnS').removeClass('hover');
		jQuery(this).find('.catIconM').removeClass('hover');
	});
	*/
}

function addCategory(e) { 
	//alert('test: ' + e);
	if(jQuery(e).attr('class') == 'entry blk') {
		jQuery(e).addClass('active');
		jQuery(e).find('.catIconM').addClass('hover');
		jQuery(e).find('.btnS span').text('Remove');
	}
	else {
		jQuery(e).removeClass('active');
		jQuery(e).find('.catIconM').removeClass('hover');
		jQuery(e).find('.btnS span').text('Add');
	}
}

function clearCategoryLightbox() { 
	jQuery('.addCategory').find('.entry').removeClass('active');
	jQuery('.addCategory').find('.catIconM').removeClass('hover');
	jQuery('.addCategory').find('.btnS span').text('Add');
}


function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
	    var arrReturnElements = new Array();
	    strClassName = strClassName.replace(/\-/g, "\\-");
	    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	    var oElement;
	    for(var i=0; i<arrElements.length; i++){
	        oElement = arrElements[i];
	        if(oRegExp.test(oElement.getAttribute("class"))){
	            arrReturnElements.push(oElement);
	        }
	    }
	    return (arrReturnElements)
}

function xtest(e){
	var x=getElementsByClassName(document,'a','active');
	var res='';
	for (var anchor = 0; anchor<x.length;anchor++){
		var item = x[anchor];res=res+','+item.rel;
		//alert(item.rel)
	}
	return res;
}

