//<?php
function print_mail (a, b)
{
    mylo = a + "@" + b;
    document . write('<a href="mailto:' + mylo + '">' + mylo + "</a>");
} 

function popupWindow(url)
{
    window . open(url, 'popupWindow', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=100,top=50,left=150')
} 

function popup_img (img, header)
{
    lnk = "popup_img.php?img=" + img + "&amp;header=" + header;
    popupWindow(lnk);
} 

function drop_down(i)
{
    var obj = document . getElementById(i);
    if (obj . style . display != "none")obj . style . display = "none";
    else obj . style . display = "inline";
} 

/*ADD HINT*/
function addHint (obj, text)
{
    $(obj) . append('<em class=\"hintTop\">' + text + '</em>');
    $(obj) . hover(function()
        {
            $(this) . find('em') . animate({
                    opacity: 'show'} , 'slow');
        } , function()
        {
            $(this) . find('em') . animate({
                    opacity: 'hide'} , 'fast');
        } 
        );
} 

/* init left menu */
function initLeftMenu()
{
    $("div.drop_down") . hide();
    $("a.menu_sup") . css ("cursor", "hand");
    $("a.menu_sup") . click(function()
        {
            $(this) . next("div") . slideToggle("slow");
        } 
        );
    l = document . location;

    $("span.menuContainer a") . each(
        function()
        {
            if (this . href == l) {
                $(this) . css ("cursor", "default");
                $(this) . parents("div") . prev("a.menu_sup") . click();
                this . href = "javascript:void(0)";
            } 
        } 
        );
} 
/*EOF init left menu */

/* init Interface */
function initTheme()
{
    initBasket ();

    /*ainmate Top menu */
    $("#upButtons  img") . hover(function() // $(this) .css ("border", "1px solid #ffcc00");
        {
            $(this) . animate({
                    height: '+=20', width: '+=20'} , 'fast');
        } , function()
        {
            $(this) . animate({
                    height: '-=20', width: '-=20' } , 'slow');
        } 
        );
} 

var basket_is_synchronized;
basket_is_synchronized = false;

function initBasket ()
{
    /* if Basket is Empty*/

    /* if Basket is not Empty*/
    var o = $("#basketCont") . offset();
    $("#basketItems") . css({
            left:o . left-250, top:o . top + 40} 
        );

    $("#basketCont") . hover (function ()
        {
            if (basket_is_synchronized == false) {
                // load
                update_basket_hover();
            } 

            $("#basketItems") . fadeIn("slow");
        } , function ()
        {
            $("#basketItems") . fadeOut("slow");
        } 
        ); //EOF $("#basketCont").hover 
    update_basket_hover(); //update on init page
} 

var basket_is_empty=true;
function update_basket_hover()
{
    $("#basketItems") . html("<img src='images/loading.gif' />");
    $ . ajax({
            type: "POST",
            url: "ses_data.php",
            data: {
                data_put:"basket", action: "get"} ,
            success: function(theResponse)
            {
                if (theResponse . indexOf ("0") == 0) {
                    // strip O;
                    $("#basket_img") . attr("src", "images/basket.gif");
                    theResponse = theResponse . substr(1);
                    basket_is_empty=true;
                } else {
                    $("#basket_img") . attr("src", "images/basket2.gif");
                    basket_is_empty=false;    
                } 
                $("#basketItems") . html(theResponse);
                basket_is_synchronized = true;
                showRightTabs();//depending on basket_is_empty value
            } 

        } ); //eof ajax
} 

function showRightTabs() {
	/*depending on basket
	 * 
	 * 1/ if page is not basket  - price
	 * 2/ if basket not empty-
	 * 			- корзина 
	 * 			- если не страница "заказать"  - заказать!
	 */
	
	/*ADD PRICE TAB*/
	var f= document.location+"";
	if (f.indexOf ("/tseny.html")==-1) $("#tsenyTab").slideDown("fast");	
	else $("#tsenyTab").hide("fast");
	
	
		if (basket_is_empty==false) {
		$("#basketTab").slideDown("fast");
			if (f.indexOf ("?object=orders&action=add")==-1)$("#orderTab").slideDown("fast");
		}else
		{$("#orderTab").hide("fast");
		$("#basketTab").hide("fast");
		}
	
}

function AnimateToBasket(ButtObj, arr)
{
    $("#basketItemsWrap li:first") . hide();

    var productIDValSplitter = (ButtObj . attr("id")) . split("_");
    var productIDVal = productIDValSplitter[1];

    var productX = $("#pic_" + productIDVal) . offset() . left;
    var productY = $("#pic_" + productIDVal) . offset() . top;

    if ($("#productID_" + productIDVal) . length > 0) {
        var basketX = $("#productID_" + productIDVal) . offset() . left;
        var basketY = $("#productID_" + productIDVal) . offset() . top;
    } else {
        var basketX = $("#basketCont") . offset() . left;
        var basketY = $("#basketCont") . offset() . top;
    } 

    var gotoX = basketX - productX;
    var gotoY = basketY - productY;

    var newImageWidth = $("#pic_" + productIDVal) . width() / 3;
    var newImageHeight = $("#pic_" + productIDVal) . height() / 3;

    var tr = $("#pic_" + productIDVal) . parents(".listCatalog tr") . clone();
    var p_price = tr . children (".t_price") . children(".money") . html();
    var p_title = tr . children (".t_title") . html();

    tr . children(".t_basket") . empty();

    tr . prependTo($("#pic_" + productIDVal) . parent())
     . css({
            'position' : 'absolute'} 
        )
     . animate({
            opacity: 0.4} , 100)
     . animate({
            opacity: 0.1, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight} , 1200, function()
        {
            $ . ajax({
                    type: "POST",
                    url: "ses_data.php",
                    data: {
                        data_put:"basket", id: productIDVal, price:p_price, title:p_title, op: "add"} ,
                    success: function(theResponse)
                    {
                        $("#basket_img") . attr("src", "images/basket2.gif");
                        basket_is_empty=false;
                        showRightTabs();
                    } 
                } 
                ); //eof ajax
            $(this) . remove(); 
            // set flag to reload ehon basket will be hover on basket
            basket_is_synchronized = false;
        } 
        );
} 

function initPriceList()
{
    /*MONEY efects */
    initMoneyCell('span.money') ;

    /* List efects*/

    $('table.listCatalog tr') . mouseover(function()
        {
            $(this) . css('background', '#FFFFE1');
        } 
        );

    $('table.listCatalog tr') . mouseout(function()
        {
            $(this) . css('background', '');
        } 
        );
    /* Order button efects*/
    $('a.addToBa') . attr ("href", "javascript:void(0);");

    $('.addToBa img') . mouseover(function()
        {
            $(this) . animate({
                    opacity:'1', height:'45'} , 'fast');
        } 
        ) . click (function()
        { 
            // addTo basket clicked
            var itemData;
            itemData = []; 
            // get item data from raw
            AnimateToBasket($(this) . parent(), itemData);
        } // EOF Onclick AddToBasket animation
        )
     . mouseout(function()
        {
            $(this) . css('background', '');
            $(this) . animate({
                    opacity:'0.6', height:'40'} , 'fast');
        } 
        );
} ///eof initPriceList function
function initMoneyCell(css)
{
    /*MONEY efects */
    $(css) . parent() . append('<span class="hint_money">UAH <span></span></span>');

    $(css) . each(function ()
        {
            var price = this . innerHTML * 1;
            var usr_curs = 1 / 8;
            var usd = price * usr_curs;
            var rur_curs = 5;
            var rur = price * rur_curs;
            var hoverText = '<img src=\"images/usd.gif\" /> ' + usd + ' USD<br/><img src=\"images/rur.gif\" /> ' + rur + ' RUR';
            addHint ($(this) . next() , hoverText);
        } 
        );
} 

function initModalWin()
{ 
    // select all the a tag with name equal to modal
    $('a[name=modal]') . click(function(e)
        { 
            // Cancel the link behavior
            e . preventDefault(); 
            // Get the A tag
            var href = $(this) . attr('href'); 
            // href can has urlencoded HTML text for win
            // or server ajax request
            // #html-
            // #ajax-
            
            if (href . indexOf('#html-') === 0) {
                // get HTML text  and put for win
                var t = href . substring(6);
                $("#dw_content") . html(t);
            } else { // load link by ajax & put into window
                // BLOCK WINDOW
                block_obj("#dw_content");
                $ . ajax({
                        type: "GET",
                        url: href, 
                        // data: { data_put:"basket", id: productIDVal, price:p_price, title:p_title, action: "add"},
                        success: function(theResponse)
                        {
                            if (theResponse . indexOf ("0") == 0) {
                                // strip O;
                                $("#basket_img") . attr("src", "images/basket.gif");
                                theResponse = theResponse . substr(1);
                               // $("#dw_content") . html(theResponse);
                                basket_is_synchronized = false;
                                update_basket_hover();
                                /*if (theResponse != "0") {
                                $("#dw_content") . html(theResponse);
                                centerModalWin();
                            } else { // emulate click if responce is 0
                                
                                $('#mask, .window') . hide();
                            } */
                                
                            } //oef if 
                            $("#dw_content") . html(theResponse);
                            centerModalWin();
                        } 
                    } 
                    ); //eof ajax
            } 
            if ($("#mask") . css("display") != "block") { // show win if hidden
                var title = $(this) . attr('title');
                $("#dw_header") . html(title);
                var id = "#dialog"; //window for all messages        
                // Get the screen height and width
                var maskHeight = $(document) . height();
                var maskWidth = $(window) . width(); 
                // Set height and width to mask to fill up the whole screen
                $('#mask') . css({
                        'width':maskWidth, 'height':maskHeight} 
                    ); 
                // transition effect
                $('#mask') . fadeIn(1000);
                $('#mask') . fadeTo("slow", 0.8);
                centerModalWin() ;
                window . scroll(0, 0); 
                // transition effect
                $(id) . fadeIn(100);
            } 
        } 
        ); 
    // if close button is clicked
    $('.window .close') . click(function (e)
        { 
            // Cancel the link behavior
            e . preventDefault();
            $('#mask, .window') . hide();
        } 
        ); 
    // if mask is clicked
    $('#mask') . click(function ()
        {
            $('.window') . hide();
            $(this) . hide();
        } 
        );
} 

function centerModalWin()
{
    var id = "#dialog"; //window for all messages        
    // Get the window height and width
    var winH = $(window) . height();
    var winW = $(window) . width(); 
    // Set the popup window to center
    $(id) . css('top', winH / 2 - $(id) . height() / 2);
    $(id) . css('left', winW / 2 - $(id) . width() / 2);
} 

function ajaxifyLinks ()
{
    initModalWin();
} 


/*
 * loads ajax to BLOCKS specofied by CSS
 */
function ajaxLoad (css, link){
	if (link==null)  $(css).html ("Link Err");
	
	   $ . ajax({
           type: "GET",
           url: link,  
           // data: { data_put:"basket", id: productIDVal, price:p_price, title:p_title, action: "add"},
           success: function(theResponse)
           {
		       $(css).html (theResponse);
           }
	   });
	
}

function removeObject(obj)
{
    obj . outerHTML = "";
    remove(obj);
} 

function block_obj(obj)
{
    var window=$(obj);
    var objH = $(window) . height();
    var objW = $(window) . width(); 
    var bl=window.prepend ("<div id='dw_blocker'><img src='images/loading.gif' /></div>");
    $("#dw_blocker").width(objW).height( objH); //.css({display:"block", position:'absolute', "background-color":'#ffffff',opacity:"0.7"});

} 
/*
 * vakldki inside forms by chekboxes
 * 
 */

var lastVkladka="";
function initVkladki (css) {
	/*init */
	$(css+" div.vkladka").hide();
	$(css+"  input[checked].vkl_switch").next().next().show();
	lastVkladka="#div"+$(css+"  input[checked].vkl_switch").attr("id");
	/*onclick*/
	$(css+" input.vkl_switch").click (function (){
	$(lastVkladka) .  slideUp("slow");
	$("#div"+this.id) .  slideDown("slow");
	lastVkladka="#div"+this.id;
	
	});

}