﻿// COMPEXPRESS CMS
// www.Nkode.pl

// Disable links
function DisableLinks() {
    $("a").attr("href", "javascript:void(0);");
    $("a").attr("target", "");
}

// ELEMENT PRINTING
var printWindow;
var printContent;
function printElement(elementId) {
    printContent = document.getElementById(elementId);
    var windowUrl = '/print.html';
    var uniqueName = new Date();
    var windowName = 'Print' + uniqueName.getTime();
    printWindow = window.open(windowUrl, windowName, 'left=100,top=100,width=720,height=400,scrollbars=1,resizable=0');
    window.setTimeout("SetHtmlAndPrint()", 500);
}

function printParent(element) {
    printContent = element.parent;
    var windowUrl = '/print.html';
    var uniqueName = new Date();
    var windowName = 'Print' + uniqueName.getTime();
    printWindow = window.open(windowUrl, windowName, 'left=100,top=100,width=720,height=400,scrollbars=1,resizable=0');
    window.setTimeout("SetHtmlAndPrint()", 500);
}

function SetHtmlAndPrint() {
    var printPlaceholder = printWindow.document.getElementById('placeholder');
    printPlaceholder.innerHTML = printContent.innerHTML;
    printWindow.document.close();
    printWindow.focus();
    printWindow.print();
    //printWindow.close();
}

// HELPERS
// COOKIES
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');

    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

//URL VARS

function getUrlVars(href) {
    var vars = [], hash;
    var hashes = href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

//FIX IMAGE PADDINGS
function FixImagePadding() {
    $('#content img').each(function () {
        var leftFloat = $(this).css('float') == 'left';
        var rightFloat = $(this).css('float') == 'right';

        if (leftFloat) {
            $(this).css({ 'marginTop': '0', 'marginRight': '10px', 'marginBottom': '10px', 'marginLeft': '0', 'float': 'left' });
        }

        if (rightFloat) {
            $(this).css({ 'marginTop': '0', 'marginRight': '0', 'marginBottom': '10px', 'marginLeft': '10px', 'float': 'right' });
        }
    });
}

//COLOR CONVERTER
//actual converter function called by main function
function toHex(N) {
    if (N == null) return "00";
    N = parseInt(N); if (N == 0 || isNaN(N)) return "00";
    N = Math.max(0, N); N = Math.min(N, 255); N = Math.round(N);
    return "0123456789ABCDEF".charAt((N - N % 16) / 16) + "0123456789ABCDEF".charAt(N % 16);
}

//function called to return hex string value
function RGBtoHEX(str) {
    //check that string starts with 'rgb'
    if (str.substring(0, 3) == 'rgb') {
        var arr = str.split(",");
        var r = arr[0].replace('rgb(', '').trim(), g = arr[1].trim(), b = arr[2].replace(')', '').trim();
        var hex = [
			toHex(r),
			toHex(g),
			toHex(b)
		];
        return "#" + hex.join('');
    }
    else {
        //string not rgb so return original string unchanged
        return str;
    }
}

//AUTO HEIGHT FOR IFRAME
function doIframe(clientId) {
    o = document.getElementsByTagName(clientId);
    for (i = 0; i < o.length; i++) {
        if (/\bautoHeight\b/.test(o[i].className)) {
            setHeight(o[i]);
            addEvent(o[i], 'load', doIframe);
        }
    }
}

function setHeight(e) {
    if (e.contentDocument) {
        e.height = e.contentDocument.body.offsetHeight + 35;
    } else {
        e.height = e.contentWindow.document.body.scrollHeight;
    }
}

function addEvent(obj, evType, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, false);
        return true;
    } else if (obj.attachEvent) {
        var r = obj.attachEvent("on" + evType, fn);
        return r;
    } else {
        return false;
    }
}

if (document.getElementById && document.createTextNode) {
    addEvent(window, 'load', doIframe);
}

/***********************************************
* IFrame SSI script II- © Dynamic Drive DHTML code library (http://www.dynamicdrive.com)
* Visit DynamicDrive.com for hundreds of original DHTML scripts
* This notice must stay intact for legal use
***********************************************/

//Input the IDs of the IFRAMES you wish to dynamically resize to match its content height:
//Separate each ID with a comma. Examples: ["myframe1", "myframe2"] or ["myframe"] or [] for none:
var iframeids = ["iframe"]

//Should script hide iframe from browsers that don't support this script (non IE5+/NS6+ browsers. Recommended):
var iframehide = "yes"

var getFFVersion = navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
var FFextraHeight = parseFloat(getFFVersion) >= 0.1 ? 16 : 0 //extra height in px to add to iframe in FireFox 1.0+ browsers

function resizeCaller() {
    var dyniframe = new Array()
    for (i = 0; i < iframeids.length; i++) {
        if (document.getElementById)
            resizeIframe(iframeids[i])
        //reveal iframe for lower end browsers? (see var above):
        if ((document.all || document.getElementById) && iframehide == "no") {
            var tempobj = document.all ? document.all[iframeids[i]] : document.getElementById(iframeids[i])
            tempobj.style.display = "block"
        }
    }
}

function resizeIframe(frameid) {
    var currentfr = document.getElementById(frameid)
    if (currentfr && !window.opera) {
        currentfr.style.display = "block"
        if (currentfr.contentDocument && currentfr.contentDocument.body.offsetHeight) //ns6 syntax
            currentfr.height = currentfr.contentDocument.body.offsetHeight + FFextraHeight;
        else if (currentfr.Document && currentfr.Document.body.scrollHeight) //ie5+ syntax
            currentfr.height = currentfr.Document.body.scrollHeight;
        if (currentfr.addEventListener)
            currentfr.addEventListener("load", readjustIframe, false)
        else if (currentfr.attachEvent) {
            currentfr.detachEvent("onload", readjustIframe) // Bug fix line
            currentfr.attachEvent("onload", readjustIframe)
        }
    }
}

function readjustIframe(loadevt) {
    var crossevt = (window.event) ? event : loadevt
    var iframeroot = (crossevt.currentTarget) ? crossevt.currentTarget : crossevt.srcElement
    if (iframeroot)
        resizeIframe(iframeroot.id);
}

function loadintoIframe(iframeid, url) {
    if (document.getElementById)
        document.getElementById(iframeid).src = url
}

if (window.addEventListener)
    window.addEventListener("load", resizeCaller, false)
else if (window.attachEvent)
    window.attachEvent("onload", resizeCaller)
else
    window.onload = resizeCaller

function HideScrollbars() {
    document.documentElement.style.overflow = 'hidden';  // firefox, chrome
    document.body.scroll = "no"; // ie only
}

function ShowScrollbars() {
    document.documentElement.style.overflow = 'auto';  // firefox, chrome
    document.body.scroll = "yes"; // ie only
}

function ScrollToElement(e, speed) {
    var targetOffset = $(e).offset().top - 10;
    $('html,body').animate({ scrollTop: targetOffset }, speed);
    //$(e).parent().effect("highlight", {}, 3000);
}

// SANATORIUM

function ShowRes(windowUrl) {
    var uniqueName = new Date();
    var windowName = 'Rezerwacja' + uniqueName.getTime();
    window.open(windowUrl, windowName, 'left=100,top=100,width=800,height=600,scrollbars=1,resizable=0');
}

function Zapytanie(token) {
    var windowUrl = "/sanatorium/rezerwacje/zapytanie.aspx?c=" + token;
    var uniqueName = new Date();
    var windowName = 'Zapytanie' + uniqueName.getTime();
    window.open(windowUrl, windowName, 'left=100,top=100,width=800,height=600,scrollbars=1,resizable=0');
}
