﻿var mouseOverImageTimeoutID;

function isPhone(source, arguments)
{
    var newString = "";
    for(var i = 0; i < arguments.Value.length; i++)
    {
        if(arguments.Value.charAt(i) >= 0 && arguments.Value.charAt(i) <= 9)
        {
            if (arguments.Value.charAt(i) != ' ')
            {
                newString += arguments.Value.charAt(i);
            }
        }
    }
    if (newString.length == 10) arguments.IsValid = true;
    else arguments.IsValid = false;
}

function isEmail(source, arguments)
{    
    var regEx = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    
    if (regEx.test(arguments.Value)) arguments.IsValid = true;
    else arguments.IsValid = false;
}



function PopUpShow(filename, modal)
{
// Show backdrop gray layer (z-index = 500)
    document.getElementById('GrayLayer').style.display ="inline"; 
    if(arguments.length == 2)
    {
        if(modal === true)
        {
            $("#GrayLayer").click(function() {
                return false;
                });
        }
        else
        {
            $("#GrayLayer").click(function() {
                PopUpHide();
                });
        }
    }
    else
    {
        $("#GrayLayer").click(function() {
            PopUpHide();
            });
    }
    
    var url = "";
    var current_div;
    var i, j;
    var current_div_count;
    var iFrame;
    var iFrameInner;
    var iFrameInnerBody;
    var iFrameInnerWidth;
    var iFrameStyle;
    var checkName;
        
    if(filename=="Privacy")
    {
        url = "/PopUps/PrivacyPolicy.aspx";
    }
    else if (filename == "DemoVideo")
    {
        url = "/PopUps/DemoVideo.aspx";
    }
    else if (filename == "none")
    {
        url = "/PopUps/Processing.aspx";
    }
    else
    {
        url = "/PopUps/" + filename;    
    }
    
    iFrame = document.getElementById("UniversalPopUpFrame");
    iFrameStyle = (iFrame.style) ? iFrame.style : iFrame;
    iFrameStyle.visibility = "hidden";

    document.getElementById("UniversalPopUp").style.display = "block";
    
    iFrame.src = url;

// Search for div objects containing Flash content
    for(i in document.getElementsByTagName('div'))
    {
        current_div = document.getElementsByTagName('div')[i];
        for (j = 0; (current_div.childNodes) && (j < current_div.childNodes.length); j++)
        {
            current_node = current_div.childNodes[j];
                    
// Hide div objects containing Flash content
            if ((current_node.tagName == "OBJECT") || (current_node.tagName == "EMBED"))
            {
                if(current_div.style.visibility != "hidden")
                {
                    hiddenDivs.push(current_div.id);
                    current_div.style.visibility = "hidden";
                }
            }
        }
    }
}

function resizeIFrame(selectedFrame)
{
// Size iframe based on content
    var iFrameInner;
    var iFrameInnerBody;
    var iFrameInnerHeight;
    var iFrameInnerWidth;
    var iFrameStyle;
    var iFrame;
    var pageContainer;
    var pageWidth;
    
    iFrame = selectedFrame;
    iFrameInner = (iFrame.contentDocument) ? iFrame.contentDocument : iFrame.contentWindow.document;
    iFrameInnerBody = (iFrameInner.documentElement) ? iFrameInner.documentElement : iFrameInner.body;
    iFrameStyle = (iFrame.style) ? iFrame.style : iFrame;

// Reset iframe size
    iFrameStyle.visibility = "hidden";
    iFrameStyle.height = 0;
    iFrameStyle.width = 0;

    iFrameInnerHeight = (iFrameInnerBody.scrollHeight) ? iFrameInnerBody.scrollHeight : iFrameInnerBody.offsetHeight;
    iFrameInnerWidth = (iFrameInnerBody.scrollWidth) ? iFrameInnerBody.scrollWidth: iFrameInnerBody.offsetWidth;
    if(iFrameInnerHeight > 650)
    {
        iFrameStyle.height = "505px";
        iFrameStyle.width = (iFrameInnerWidth + 20) + "px";
    }
    else
    {
        iFrameStyle.height = (iFrameInnerHeight)+ "px";
        iFrameStyle.width = (iFrameInnerWidth + 2) + "px";
    }

// Place iframe in center of page
    document.getElementById("UniversalPopUp").style.position = "fixed";
    document.getElementById("UniversalPopUp").style.top = "5%";
    pageWidth = (document.documentElement.clientWidth) ? (document.documentElement.clientWidth) : document.body.clientWidth;
    document.getElementById("UniversalPopUp").style.left = (pageWidth/2 - iFrameInnerWidth/2) + "px";
    iFrameStyle.visibility = "visible";
}

function PopUpHide()
{
// Show backdrop gray layer (z-index = 500)
   var i, j, k;
   var current_div;
   var current_node;

// Hide all div objects containing iFrames
    for(i in document.getElementsByTagName('div'))
    {
        current_div = document.getElementsByTagName('div')[i];

    // Hide backdrop gray layer (z-index = 500)
        if(current_div.id == "GrayLayer")
        {
            current_div.style.display = "none";
        }

        for (j = 0; (current_div.childNodes) && (j < current_div.childNodes.length); j++)
        {
            current_node = current_div.childNodes[j];
            if ((current_node == "[object HTMLIFrameElement]") && (current_node.src.indexOf("http") == -1))
            {
                current_div.style.display = "none";
            }
        }
    }
    
// Show any div objects hidden due to Flash content
    if (hiddenDivs.length > 0)
    {
        for(k = 0; k < hiddenDivs.length; k++)
        {
            document.getElementById(hiddenDivs[k]).style.visibility = "visible";
        }
    }
    
// Reset iframe source (allows for proper resizing)
    document.getElementById("UniversalPopUpFrame").src = "";
    resizeIFrame(document.getElementById("UniversalPopUpFrame"));
}

function ASFH(id)
{
    AutoSetFraimHight(id);
}

function GetURLParm(name)
{
  name = name.toLowerCase();
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href.toLowerCase());
  if( results == null )
    return "";
  else
    return results[1];
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function FitImgInBox(img, maxHeigh, maxWidth)
{
    //Fit Inside imagarty box
    var imgFile = new Image();
    imgFile.src = img.src;
    var height = imgFile.height-0;
    var width = imgFile.width-0;

    //Try to Fit it one way
    var newHeight = maxHeigh-0;
    var newWidth = ((maxHeigh-0) * width / height)-0;

    //If not the other
    if (newWidth > maxWidth-0) 
    {
      newHeight = ((maxWidth-0) * height / width);
      newWidth = maxWidth;
    }

    img.style.height = newHeight + 'px';
    img.style.width = newWidth + 'px';
}

function AutoSetFrameHeight(id)
{
    AutoSetFraimHight(id);
}

function AutoSetFraimHight(frameid)
{
    
    if (window.navigator.userAgent.indexOf('Safari') > 0) 
    {          
          
        var newheight;
        var ele = document.getElementById(frameid);
        newheight = ele.contentWindow.document.body.offsetHeight;
        ele.height = newheight+"px";
    }
    else //firefox and IE
    {
        var newheight;
        var ele = document.getElementById(frameid);
        newheight = ele.contentWindow.document.body.scrollHeight;
        ele.height = newheight + "px";
    }
}

function showIFrame()
{
    // Show Chat link after content loads
    $('.ChatFrame').show();
}

function KeyPress(e)
{
    var key;
    if (window.event)
    {
	key = window.event.keyCode;
    }
    else
    {
        key = e.which;
    }
    return (key != 13);
}

function CPI_PopUpShow(fileName)
{
// Show backdrop gray layer (z-index = 500)
    document.getElementById('CPI_GrayLayer').style.display ="inline"; 
    
    var url = "";
    var current_div;
    var i, j;
    var current_div_count;
    var iFrame;
    var iFrameInner;
    var iFrameInnerBody;
    var iFrameInnerWidth;
    var iFrameStyle;
    var checkName;
        
    url = "/PopUps/" + fileName;
    
    iFrame = document.getElementById("CPI_PopUpFrame");
    iFrameStyle = (iFrame.style) ? iFrame.style : iFrame;

    iFrameStyle.visibility = "hidden";

    document.getElementById("CPI_PopUp").style.display = "block";
    
    iFrame.src = url;
    
// Search for div objects containing Flash content
    for(i in document.getElementsByTagName('div'))
    {
        current_div = document.getElementsByTagName('div')[i];
        for (j = 0; (current_div.childNodes) && (j < current_div.childNodes.length); j++)
        {
            current_node = current_div.childNodes[j];
                    
// Hide div objects containing Flash content
            if ((current_node.tagName == "OBJECT") || (current_node.tagName == "EMBED"))
            {
                if(current_div.style.visibility != "hidden")
                {
                    hiddenDivs.push(current_div.id);
                    current_div.style.visibility = "hidden";
                }
            }
        }
    }
}

function CPI_PopUpHide()
{
   var i;
   var current_div;

// Cycle through div tags
    for(i in document.getElementsByTagName('div'))
    {
        current_div = document.getElementsByTagName('div')[i];

    // Hide backdrop gray layer (z-index = 500) and CPI popup
        if(current_div.id == "CPI_GrayLayer" || current_div.id == "CPI_PopUp")
        {
            current_div.style.display = "none";
        }
    }
}

function resizeCPIPopUp(selectedFrame)
{
// Size iframe based on content
    var iFrameInner;
    var iFrameInnerBody;
    var iFrameInnerHeight;
    var iFrameInnerWidth;
    var iFrameStyle;
    var iFrame;
    var pageContainer;
    var pageWidth;
    
    iFrame = selectedFrame;
    iFrameInner = (iFrame.contentDocument) ? iFrame.contentDocument : iFrame.contentWindow.document;
    iFrameInnerBody = (iFrameInner.documentElement) ? iFrameInner.documentElement : iFrameInner.body;
    iFrameStyle = (iFrame.style) ? iFrame.style : iFrame;

// Reset iframe size
    iFrameStyle.visibility = "hidden";
    iFrameStyle.height = 0;
    iFrameStyle.width = 0;

    iFrameInnerHeight = (iFrameInnerBody.scrollHeight) ? iFrameInnerBody.scrollHeight : iFrameInnerBody.offsetHeight;
    iFrameInnerWidth = (iFrameInnerBody.scrollWidth) ? iFrameInnerBody.scrollWidth: iFrameInnerBody.offsetWidth;
    if(iFrameInnerHeight > 650)
    {
        iFrameStyle.height = "505px";
        iFrameStyle.width = (iFrameInnerWidth + 20) + "px";
    }
    else
    {
        iFrameStyle.height = (iFrameInnerHeight)+ "px";
        iFrameStyle.width = (iFrameInnerWidth + 2) + "px";
    }

// Place iframe in center of page
    document.getElementById("CPI_PopUp").style.position = "fixed";
    document.getElementById("CPI_PopUp").style.top = "5%";
    pageWidth = (document.documentElement.clientWidth) ? (document.documentElement.clientWidth) : document.body.clientWidth;
    document.getElementById("CPI_PopUp").style.left = (pageWidth/2 - iFrameInnerWidth/2) + "px";
    iFrameStyle.visibility = "visible";    
}

function encodeHTML(source, arguments)
{    
    var newString = arguments.Value;
    
    newString = newString.replace("<", "&lt;");
    newString = newString.replace(">", "&gt;");
    arguments.Value = newString;
    // alert($("#" + source.controltovalidate).val());
    $("#" + source.controltovalidate).val(newString);
    // alert($("#" + source.controltovalidate).val());
    if(newString.length > 0) arguments.IsValid = true;
    else arguments.IsValid = false;
}

function htmlEncode(source)
{
    var newString = $(source).val();
    newString = newString.replace(/</gi, "&lt;");
    newString = newString.replace(/>/gi, "&gt;");
    $(source).val(newString);
}

function htmlEncode(source, hiddenFieldName)
{
    var newString = $(source).val();
    var sourceID = $(source).attr("id");
    var hiddenField = $("input[id*=" + hiddenFieldName + "]");
    newString = newString.replace(/</gi, "&lt;");
    newString = newString.replace(/>/gi, "&gt;");
    $(hiddenField).val(newString);
}

function isNumberKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) return false;
    
    return true;
}

function Trim(txt)
{
    return txt.replace(/^\s+|\s+$/g,"");
}

var thumbnailSource = null;
var desiredZoomWidth = 0;
var desiredZoomHeight = 0;
var thumbnailTop = 0;
var thumbnailLeft = 0;

function showMouseOverImage(thumbnailImage, width, height)
{
    // IE does not allow for variables to be sent to setTimeout. Global variable are set and used.
    thumbnailSource = thumbnailImage.attr("src");
    desiredZoomWidth = width;
    desiredZoomHeight = height;
    thumbnailTop = thumbnailImage.offset().top;
    thumbnailLeft = thumbnailImage.offset().left;

    mouseOverImageTimeoutID = window.setTimeout(delayedDisplay, 500);
}

function delayedDisplay(thumbnailImage, width, height)
{
    var zoomedImage = new Image();
    var zoomedImageContainer = $("#MouseOverImageContainer");
    
    zoomedImage.src = thumbnailSource;
    FitImgInBox(zoomedImage, desiredZoomWidth, desiredZoomHeight);
    $("#MouseOverImage").attr("src", thumbnailSource);
    $("#MouseOverImage").height(zoomedImage.style.height);
    $("#MouseOverImage").width(zoomedImage.style.width);
    $("#MouseOverImage").show();
    
    var zoomedImageX = thumbnailLeft;
    var zoomedImageY = thumbnailTop;
    
    zoomedImageX =  zoomedImageX + 30;
    zoomedImageY = (zoomedImageY - 20) - parseInt(zoomedImage.style.height);
    
    zoomedImageContainer.css("top", zoomedImageY + "px");
    zoomedImageContainer.css("left", zoomedImageX + "px");
    
    zoomedImageContainer.show();
}

function hideMouseOverImage()
{
    window.clearTimeout(mouseOverImageTimeoutID);
    
    $("#MouseOverImage").attr("src", "");
    
    $("#MouseOverImageContainer").hide();
    
}

$(document).ready(function() {

    // For thepros.com, highlight active tab
    if(window.location.toString().indexOf("thepros.com") > 0)
    { 
        if(window.location.toString().indexOf("/photo/") > 0)
        {
            $("a.photography").css("background-image", "url('/images/Header/bg-photography.gif')");
            $("a.photography").css("background-position", "bottom");
            $("a.photography").css("color", "#1A7CAA");
        }
        else if(window.location.toString().indexOf("/video/") > 0)
        {
            $("a.video").css("background-image", "url('/images/Header/bg-video.gif')");
            $("a.video").css("background-position", "bottom");
            $("a.video").css("color", "#1A7CAA");
        }
        else if(window.location.toString().indexOf("/dj/") > 0)
        {
            $("a.disc").css("background-image", "url('/images/Header/bg-disc.gif')");
            $("a.disc").css("background-position", "bottom");
            $("a.disc").css("color", "#1A7CAA");
        }
    }
});

