﻿function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; }
    }
}

function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}
function DropDown(imageId, rowId) {
    var actualImage = document.getElementById(imageId);
    var actualRow = document.getElementById(rowId);
    //alert(actualImage.src);
    if (actualRow.style.visibility == "visible") {

        actualImage.src = "images/plus.gif";
        actualRow.style.visibility = "hidden"
        actualRow.style.position = "absolute";
    }
    else {
        actualImage.src = "images/minus.gif";
        actualRow.style.visibility = "visible";
        actualRow.style.position = "relative";
        actualRow.style.textIndent = "0";
    }
}

String.prototype.endsWith = function(str) {
return (this.match(str+"$")==str);
}

function MailSender(width, height, subject) {
    var newWindow = window.open("", "pictureViewer", "location=no, directories=no, fullscreen=no, menubar=no, status=no, toolbar=no, width=" + width + ", height=" + height + ", scrollbars=yes");
    var urlBase = window.location.href;
    if (urlBase.indexOf("?") != -1) {
        urlBase = urlBase.substring(0, urlBase.indexOf("?"));
    }
    urlBase = urlBase.substring(0, urlBase.lastIndexOf("/"));
    if (!urlBase.endsWith("/"))
        urlBase = urlBase + "/";
    var newLocation = urlBase + "sendtofriend.aspx?url=" + escape(window.location) + "&subject=" + escape(subject);
    newWindow.location = newLocation;
}

function SetMailSenderLinkOnEmbedings() {
    var embeds = document.embeds;
    var showItem = true;
    for (var i = 0; i < embeds.length; i++) {
        showItem = true;
        //alert(embeds[i].outerHTML);
        if (embeds[i].getAttribute("pearl") != null) {
            if (embeds[i].getAttribute("pearl").indexOf("v-pearl-ad") != -1) {
                showItem = false;
            }
        }
        if (showItem) {
            var textElement = document.createElement("a");
            textElement.setAttribute("href", "javascript:MailSender(584, 470, 'V-Pearl Video');");
            var imgTag = document.createElement("img");
            imgTag.setAttribute("src", "images/send.gif");
            imgTag.setAttribute("alt", "Küldje el barátjának, ismerősének");
            imgTag.setAttribute("class", "sendmail");
            textElement.appendChild(imgTag);
            textElement.appendChild(document.createTextNode("Küldje el barátjának, ismerősének"));
            embeds[i].parentNode.insertBefore(textElement, embeds[i].nextSibling);
            embeds[i].parentNode.insertBefore(document.createElement("br"), textElement);
        }
    }
}

function AdminMail(userid) {
    var newWindow = window.open("", "adminMail", "location=no, directories=no, fullscreen=no, menubar=no, status=no, toolbar=no, width=" + 584 + ", height=" + 470 + ", scrollbars=yes");
    newWindow.location = "http://www.v-pearl.hu/AdminMailApplicants.aspx?id=" + userid;
}

function PrintPage() {
    if (window.location.href.indexOf("printable=1") != -1)
        window.print();
}

function LoadAd() {
    GetAdvertisement(1, "topbanner", null, "ads1", 2);
    GetAdvertisement(2, "leftbanner", null, "ads2", 2);
    GetAdvertisement(3, "rightbanner", null, "ads3", 2);
}

function LoadSlider() {
    if (window.location.href.indexOf("printable=1") == -1) {
        $(".WebShopSlider").append("<div style='padding-top: 7px; width: 186px; height:1100px; overflow: hidden;'><iframe id='sliderFrame' src='http://service.v-pearl.hu/SliderTestPage.html?parent=" + encodeURIComponent(window.location.href) + "' marginheight='0' marginwidth='0' height='1100' width='186' scrolling='no' frameborder='0' style='overflow: hidden;'></iframe></div>");
    }
}

//function LoadForeignAds() {
//    $.get("ForeignAds.xml", function (data) {
//        $(data).find("Ad").each(function () {
//            var $ad = $(this);
//            var newId = $ad.attr("type") + "-" + $ad.attr("title") + "-" + Math.floor(Math.random() * 1000000);
//            $("img[src*='" + $ad.attr("width") + "x" + $ad.attr("height") + ".png']").each(function () {
//                var $image = $(this);
//                $image.replaceWith("<div style='width:" + $ad.attr("width") + "px;height:" + $ad.attr("height") + "px'><div id='" + newId + "'>" + $image.outer() + "</div></div>");
//                $frame = $("#" + newId);
//                $frame.empty();
//                var omFrame = document.getElementById(newId);
//                var script = document.createElement("script");
//                script.type = "text/javascript";
//                script.id = "js" + newId;
//                script.text = "\r\nalert('alma');adsense(\"" + $ad.attr("client") + "\", \"" + $ad.attr("id") + "\", " + $ad.attr("width") + ", " + $ad.attr("height") + ");\r\n";
//                omFrame.appendChild(script);

////                $frame.fadeOut("slow", function () {
////                    if ($ad.attr("type") == "Google-AdSense") {
////                        //                        var omFrame = document.getElementById(newId);
////                        //                        var script = document.createElement("script");
////                        //                        script.type = "text/javascript";
////                        //                        script.id = "js" + newId;
////                        //                        script.text = "<!--\r\ngoogle_ad_client=\"" + $ad.attr("client") + "\";\r\n/*" + $ad.attr("title") + "*/\r\ngoogle_ad_slot=\"" + $ad.attr("id") + "\";\r\ngoogle_ad_width=" + $ad.attr("width") + ";\r\ngoogle_ad_height=" + $ad.attr("height") + ";\r\n//-->";
////                        //                        
////                        //                        
////                        //                        var script2 = document.createElement("script");
////                        //                        script2.type = "text/javascript";
////                        //                        script2.id = "js2" + newId;
////                        //                        script2.src = "http://pagead2.googlesyndication.com/pagead/show_ads.js";
////                        //                        omFrame.appendChild(script);
////                        //                        omFrame.appendChild(script2);


////                        
////                        $frame.fadeIn("slow");
////                    }
////                });
//            });
//        });
//    });
//}

$.fn.outer = function (val) {
    if (val) {
        $(val).insertBefore(this);
        $(this).remove();
    }
    else { return $("<div>").append($(this).clone()).html(); }
}


function adsense(cli,  slot, w, h) {
    google_ad_client = cli;
    google_ad_slot = slot;
    google_ad_width = w;
    google_ad_height = h;

    var url = "http://pagead2.googlesyndication.com/pagead/show_ads.js";
    document.write('<sc' + 'ript src="' + url + '">' + '</sc' + 'ript>');
}

/*RATES*/

var rateTimer;

function initRateTimer()
{
    var needTimerJob = false;
    $("img[src*='fresh-rates.gif']").each(function () {
        var $image = $(this);
        $image.replaceWith("<div id='CurrentRateDiv'>" + $image.outer() + "</div>");
        needTimerJob = true;
    });
    if (needTimerJob) {
        rateTimer = setTimeout("refreshRates()", 1);
    }
}

var rateContentTdStart = "<td class='RateTableColumn' title='{title}'>";
var rateContentTdEnd = "</td>";
var rateHeadTdStart = "<th class='RateTableFirstColumn'>";
var rateHeadTdEnd = "</th>";
var auMoveTemplate = "&nbsp;<img src='UserFiles/Image/Rates/nyil{auMove}.png' alt='' />";
var agMoveTemplate = "&nbsp;<img src='UserFiles/Image/Rates/nyil{agMove}.png' alt='' />";

function refreshRates() {
    clearTimeout(rateTimer);
    var currentRatesTable = "";
    var url = '/rateservice.svc/GetCurrentRates?' + Math.random() * 100;
    currentRatesTable = "<table id='currentRateTable'  class='tablecloth'>";
    currentRatesTable = currentRatesTable + "<tr><td class='RateTableTopLeftCell'>{Date}</td><th class='RateTableFirstRow' title='színarany'>Au</th><th class='RateTableFirstRow' title='18 kt-os arany'>Au750</th><th class='RateTableFirstRow' title='14 kt-os arany'>Au585</th><th class='RateTableFirstRow' title='14 kt-os törtarany'>14kt-os törtarany</th><th class='RateTableFirstRow' title='színezüst'>Ag</th><th class='RateTableFirstRow' title='800-as ezüst'>Ag800</th></tr>";
    var au, au750, au585, ag, ag800, autort, date, time, agMove, auMove;
    $.getJSON(url, function (jsonResult) {
        $.each(jsonResult, function (key, val) {
            currentRatesTable = currentRatesTable + "<tr>";
            $.each(val, function (innerKey, innerVal) {
                switch (innerKey) {
                    case "AU":
                        au = rateContentTdStart.replace("{title}", "színarany") + innerVal + auMoveTemplate + rateContentTdEnd;
                        break;
                    case "AU750":
                        au750 = rateContentTdStart.replace("{title}", "18 kt-os arany") + innerVal + auMoveTemplate + rateContentTdEnd;
                        break;
                    case "AU585":
                        au585 = rateContentTdStart.replace("{title}", "14 kt-os arany") + innerVal + auMoveTemplate + rateContentTdEnd;
                        break;
                    case "AU5852":
                        autort = rateContentTdStart.replace("{title}", "14 kt-os törtarany") + innerVal + auMoveTemplate + rateContentTdEnd;
                        break;
                    case "AG":
                        ag = rateContentTdStart.replace("{title}", "színezüst") + innerVal + agMoveTemplate + rateContentTdEnd;
                        break;
                    case "AG800":
                        ag800 = rateContentTdStart.replace("{title}", "800-as ezüst") + innerVal + agMoveTemplate + rateContentTdEnd;
                        break;
                    case "Time":
                        time = rateHeadTdStart + innerVal + rateHeadTdEnd;
                        break;
                    case "Date":
                        date = innerVal;
                        break;
                    case "AUMove":
                        auMove = innerVal;
                        break;
                    case "AGMove":
                        agMove = innerVal;
                        break;
                }
            });
            currentRatesTable = currentRatesTable + time + au + au750 + au585 + autort + ag + ag800;
            currentRatesTable = currentRatesTable.replace(/{auMove}/gi, auMove).replace(/{agMove}/gi, agMove) + "</tr>";
        });
        currentRatesTable = currentRatesTable + "</table>";
        currentRatesTable = currentRatesTable.replace("{Date}", date);
        $("#CurrentRateDiv").html(currentRatesTable);
        tablecloth();
        //$("#CurrentRateDiv").attr("style", "width:" + $("#CurrentRateDiv").children().width() + "px;height:" + $("#CurrentRateDiv").children().height() + "px;");
        rateTimer = setTimeout("turnOnRefreshSign()", 45000);
    });
}


function turnOnRefreshSign() {
    clearTimeout(rateTimer);
    rateTimer = setTimeout("refreshRates()", 100);
}
