
var addthis_config = {
	ui_language: 'ru',
	services_exclude: 'www.odnoklassniki.ru,moikrug.ru',
	services_compact: 'vk,facebook,livejournal,twitter,blogger,favorites,email,print,more',
	services_custom : [{
		name	: 'Одноклассники',
		url		: 'http://www.odnoklassniki.ru/dk?st.cmd=addShare&st._surl=' + encodeURIComponent(location.href) + '&title=' + encodeURIComponent(document.title),
		icon	: '/images/cms/eip/odnoklassniki.png'
	}, {
		name	: 'Мой круг',
		url		: 'http://moikrug.ru/share?url=' + encodeURIComponent(location.href) + '&title=' + encodeURIComponent(document.title) + '&description=',
		icon	: '/images/cms/eip/moikrug.png'
	}]
}


$(document).ready(function() {

	insertSocialButtons('#social_block');

	//Events
	//Custom quick buy button
	if (isSetBuyHandler()) {
		$('a.button_buy').click(function() {
			var sRequest = $(this).attr('href');
			$('a.button_buy').each(function() {
				$(this).attr('disabled', 'disabled');
				$(this).unbind('click');
				$(this).click(function() { return false; });
			});
			$.get(sRequest, function(){
				document.location = "/emarket/cart/";
			});
			return false;
		});
	}
	
	$('form.catalog_filters .filter_anchor').click(function() {
		var sFieldName = $(this).attr('id');
		sFieldName = sFieldName.substr(4);
		
		$('#field_' + sFieldName).remove();
		
		var arParts = $(this).attr('href');
		arParts = arParts.split('=');
		
		var sHiddenControl = '<input type="hidden" id="field_'+sFieldName+'" name="fields_filter['+sFieldName+']" value="'+arParts[1]+'" />';
		$('form.catalog_filters').append(sHiddenControl);
		
		$('form.catalog_filters').submit();
		
		return false;
	});
	
	$('.layout_control a').click(function() {
		var sMode = $(this).attr('id');
		sMode = sMode.substr(7);
		var oDate = new Date(2100, 5, 5, 5, 5, 5);
		var sExpires = oDate.toGMTString();
		var sPath = '/';
		setCookie('catalogLayout', sMode, sExpires, sPath);
		document.location.href = document.location.href;
	});
	
	$('#catalog_perpage').change(function() {
		var sPerPage = $(this).val();
		var oDate = new Date(2100, 5, 5, 5, 5, 5);
		var sExpires = oDate.toGMTString();
		var sPath = '/';
		setCookie('catalogLayoutPerpage', sPerPage, sExpires, sPath);
		var arUrl = document.location.href.split('?');
		var sRefreshUrl = arUrl[0];
		if (arUrl.lenght > 1) {
			sRefreshUrl += '?' + arUrl[1].replace(/[\&\?]?p=[0-9]+/g, '');
		}
		document.location.href = sRefreshUrl.replace('?&', '?');
	});
	
	$('#order_name').click(function() {
		$('#order').attr('name', 'order_filter[name]');
		$('form.catalog_filters').submit();
		return false;
	});
	
	$('#order_price').click(function() {
		$('#order').attr('name', 'order_filter[price]');
		$('form.catalog_filters').submit();
		return false;
	});

	function isSetBuyHandler() {
		if(typeof(window['uPageEditor']) == "undefined") {
			return true;
		} else {
			var oEip = uPageEditor.get();
			return !(oEip.isEnabled());
		}
	}
	
	function insertSocialButtons(sWrapperId) {
		$(sWrapperId).append('<a class="social_button social_vk" rel="nofollow" href="http://vkontakte.ru" title="Поделиться ВКонтакте" onclick="window.open(\''+putSocialHref('vk')+'\', \'_blank\', \'width=640,height=480\');return false;"></a>');
		$(sWrapperId).append('<a class="social_button social_odnoklassniki" rel="nofollow" href="http://odnoklassniki.ru" title="Поделиться в Одноклассниках" onclick="window.open(\''+putSocialHref('odnoklassniki')+'\', \'_blank\', \'width=640,height=480\');return false;"></a>');
		$(sWrapperId).append('<a class="social_button social_krug" rel="nofollow" href="http://moikrug.ru" title="Поделиться в Моём круге" onclick="window.open(\''+putSocialHref('moikrug')+'\', \'_blank\', \'width=1000,height=600\');return false;"></a>');// moikrug
		$(sWrapperId).append('<a class="social_button social_mir" rel="nofollow" href="http://my.mail.ru" title="Поделиться в Моём мире" onclick="window.open(\''+putSocialHref('moimir')+'\', \'_blank\', \'width=640,height=480\');return false;"></a>');
		$(sWrapperId).append('<a class="social_button social_facebook" rel="nofollow" href="http://facebook.com" title="Поделиться в Facebook" onclick="window.open(\''+putSocialHref('facebook')+'\', \'_blank\', \'width=640,height=480\');return false;"></a>');
		$(sWrapperId).append('<a class="social_button social_twitter" rel="nofollow" href="http://twitter.com" title="Поделиться в Twitter" onclick="window.open(\''+putSocialHref('twitter')+'\', \'_blank\', \'width=640,height=480\');return false;"></a>');
		$(sWrapperId).append('<a class="social_button social_livejournal" rel="nofollow" href="http://livejournal.com" title="Поделиться в LiveJournal" onclick="window.open(\''+putSocialHref('livejournal')+'\');return false;"></a>');
		$(sWrapperId).append('<a class="social_button social_liveinternet" rel="nofollow" href="http://liveinternet.ru" title="Поделиться в LiveInternet" onclick="window.open(\''+putSocialHref('liveinternet')+'\');return false;"></a>');

		$(sWrapperId).append('<div class="cleaner" />');
	}
	
	function putSocialHref(sType) {
		var oTitle = document.documentElement.getElementsByTagName( "h1" ).item(0);
		var sTitle = '';
		if (oTitle) {
			sTitle = document.documentElement.getElementsByTagName( "h1" ).item(0).innerHTML;
		}
		sTitle = (sTitle == '') ? document.title : sTitle;
		var sUrl = removeAParams(location.href);
		switch(sType) {
			case 'vk': {
				return 'http://vkontakte.ru/share.php?url='+encodeURIComponent(sUrl);
			} break;
			case 'odnoklassniki': {
				return 'http://www.odnoklassniki.ru/dk?st.cmd=addShare&st._surl=' + encodeURIComponent(sUrl) + '&title=' + encodeURIComponent(sTitle);
			} break;
			case 'moikrug': {
				return 'http://moikrug.ru/share?url=' + encodeURIComponent(sUrl) + '&title=' + UTF8_to_CP1251_URL(sTitle) + '&description=';
			} break;
			case 'moimir': {
				return 'http://connect.mail.ru/share?url=' + encodeURIComponent(sUrl) + '&title=' + encodeURIComponent(sTitle);
			} break;
			case 'facebook': {
				return 'http://www.facebook.com/sharer.php?u=' + encodeURIComponent(sUrl);
			} break;
			case 'twitter': {
				return 'http://twitter.com/home?status=' + encodeURIComponent(sTitle) + '+' + encodeURIComponent(sUrl);
			} break;
			case 'livejournal': {
				return 'http://www.livejournal.com/update.bml?event=' + encodeURIComponent(sUrl) + '&subject=' + encodeURIComponent(sTitle);
			} break;
			case 'liveinternet': {
				return 'http://www.liveinternet.ru/journal_post.php?action=n_add&cnurl=' + encodeURIComponent(sUrl);
			} break;
		}
		return '';
	}
});

function removeAParams(sUrl) {
	var sResult = sUrl.replace(/u\-login=([a-zA-Z0-9\-]+)\&?/g, '');
	var sResult = sResult.replace(/u\-password=([a-zA-Z0-9\-]+)\&?/g, '');
	var sResult = sResult.replace(/u\-password\-md5=([a-zA-Z0-9\-]+)\&?/g, '');
	var sResult = sResult.replace(/u\-login\-store=([a-zA-Z0-9\-]+)\&?/g, '');
	var sResult = sResult.replace(/\?$/g, '');
	
	return sResult;
}

function UTF8_to_CP1251_URL(sValue) {
    var    text = "", Ucode, ExitValue, s;
    for (var i = 0; i < sValue.length; i++) {
        s = sValue.charAt(i);
        Ucode = s.charCodeAt(0);
        var Acode = Ucode;
        if (Ucode > 1039 && Ucode < 1104) {
            Acode -= 848;
            ExitValue = "%" + Acode.toString(16);
        }
        else if (Ucode == 1025) {
            Acode = 168;
            ExitValue = "%" + Acode.toString(16);
        }
        else if (Ucode == 1105) {
            Acode = 184;
            ExitValue = "%" + Acode.toString(16);          
        } 
        else if (Ucode == 32) {
            Acode = 32;
            ExitValue = "%" + Acode.toString(16);          
        } 
        else if (Ucode == 10){
            Acode = 10;
            ExitValue = "%0A";
        }
        else {
            ExitValue = s;
        }
        text = text + ExitValue; 
    }      
    return text; 
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function setCookie (name, value, expires, path, domain, secure) {
	document.cookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}
